I have a string in groovy that I make from other strings, example:
def final PREFIX = "myprefix"
def prefix2 = PREFIX + ".whatever1"
Now suppose I have a HashMap
and I want to do a lookup using prefix2
as part of a key:
HashMap<String,String> map = new HashMap<String,String>()
map.put("myprefix.whatever1.value","aaa")
If I do:
def key = "${prefix2}.value"
String result=(map.get(key))
Then result = null
, but if I do:
String key="${prefix2}.value"
String result=(map.get(key))
Then result = aaa
.
I can understand why this happens, obviously a type inference issue. But I still find it makes me feel icky. Something just doesn't "feel right" about it. Do you know what I mean?
Is this type of thing normal and should be "expected"? Am I asking for too much for Groovy to know that if I created the string using quotes, it should work when being used to look up a value in a Hashmap <String, String>
without being defined as a String
object reference? Is this a bug or a feature?