With the following snippet I cannot retrieve gString
from a map:
def contents = "contents"
def gString = "$contents"
def map = [(gString): true]
assert map.size() == 1 // Passes
assert gString.hashCode() == map.keySet().first().hashCode() // Passes, same hash code
assert map[gString] // Fails
How on earth is that possible?
Assertion message clearly shows that there's something seriously wrong with Groovy:
assert map[gString] // Fails
| ||
| |contents
| null
[contents:true]
It's not the same question as Why groovy does not see some values in dictionary? First answer there suggests:
You're adding GString instances as keys in your map, then searching for them using String instances.
In this question I clearly add GString
and try to retrieve GString
.
Also neither Why are there different behaviors for the ways of addressing GString keys in maps? nor Groovy different results on using equals() and == on a GStringImpl have an answer for me. I do not mutate anything and I do not mix String
with GString
.
String
withGString
. In my code I do not mutate or mix anything. – Firemanmap[gString]
invoked the category methodDefaultGroovyMethods.getAt(Object,String)
(with theGString
coerced toString
, of course), rather than the expected (at least by me)DefaultGroovyMethods.getAt(getAt(Map<K,V>,K)
. I don't know how the Groovy runtime engine came to make such a decision, but it seems to me that it is the wrong decision. – Similitudemap[gString]
withmap.getAt(gString)
, then it still fails the same way. However, if you replace it withDefaultGroovyMethods.getAt(map, gString)
, then you get what seems to me to be the correct behavior. – Similitude