-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static Type Checking on Map Iteration Shows Incorrect Compile Errors #872
Comments
This has to do with #863 and the linked Groovy bugs. The way STC behaves in this situation is not very intuitive IMO due to flow typing. In the statement For best compatibility with current and future STC and compilers/editors, I suggest writing your example like this: def props = (Map<String, Object>) ois.readObject()
props.each { entry ->
setProperty(entry.key, entry.value)
} In this form, the type for |
Thanks for the followup! I will read those links a bit more in detail, but just wanted to relate that I'm not sure this is correct behavior as even with your suggestions, I am still getting the error - here is a contrived example: @CompileStatic
void compileBug() {
Map m = [foo: 'cat', bar: 'hat']
def x = (Map<String,String>)m
x.each { e ->
println(e.value)
}
} In this case, the error is:
As I mentioned before - this all used to compile fine in Eclipse until some recent update, and it still compiles fine with groovyc. I would also tentatively note it seems to work ok with the 2.5 compiler (unfortunately, I'm stuck using 2.4 for this project). Could it be a regression related to the changes to handle 2.4 in #863? |
Apologies, one more update: I found the following does work - @CompileStatic
private void compileBug() {
Map m = [foo: 'cat', bar: 'hat']
def x = (Map<String,Object>)m // <== instead of Map<String,String>
x.each { e ->
println(e.value)
}
} ie: the trouble seems to be coming from having anything other than Object as the second generic type parameter. The first type parameter seems to be correctly inferred and respected inside the closure. |
There is definitely something strange going on here because the inferred type of the @CompileStatic
void meth() {
def map = [foo: 'cat', bar: 'hat']
map.each { entry -> // entry seen as Map<String,String>
println entry.value // entry seen as Map<String,Object>
}
} |
Compatibility check of the DGM is breaking down because typeof(map) is |
It appears the extension method cache is getting polluted somehow. |
It looks like any DGM that uses |
ready to test |
Fantastic - thank you for working on it! Let me know if anything I can do to help / test / etc. |
Yes, please update to the latest snapshot and try out your source/project with errors. |
It works! All the errors have gone away. Thanks so much! |
In the last few days my existing code is suddenly showing compile errors whenever I have enabled static compilation and a Map is being iterated. For example:
This code results in this error:
This is happening across many different instances of iterating over Maps, it is occurring with both the two argument closure and single argument closure. The same code compiles successfully when compiled with
groovyc
.The only way I seem to be able to get things to work is to remove iteration using closures from the code.
I am running this version of groovy-eclipse:
I am using the 2.4 groovy compiler.
The text was updated successfully, but these errors were encountered: