Last active
March 4, 2016 02:31
-
-
Save robfletcher/d9207087ca9665bfa0da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import groovy.transform.* | |
| @CompileStatic | |
| Map<String, ?> makeMap() { | |
| def map = [string: "string"] | |
| map.number = 1 | |
| return map | |
| } | |
| def map = makeMap() | |
| map.each { println it.value.getClass().name } |
Author
This is weird. I have also noticed it works for initialization like below:
def map = [string: 'string', numbers: [1, 2, 3, 4].collect { [n: it] } ]
println map.number.getClass().name
immediately after line 6 prints as String.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This surprised me – the method returns a
Map<String, String>having seemingly inferred the value type from the initial assignment. When the integer value is added it gets coerced to String. This happens with any type (I stumbled on it while adding aList<Map<String, String>>to a map in a similar way).Things to try:
@CompileStaticdefwithMap<String, ?>on line 5defwithMap<String, Object>on line 5[:]as Map<String, ?>at the end of line 5map.string = "string"