使用以下代码片段我无法从 map 中检索 gString
:
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 gString.is(map.keySet().first()) // Passes, exactly the same object
assert map[gString] // Fails
这怎么可能?
这里有趣的是 map.get(map.keySet()[0])
工作正常而 map.get[map.keySet()[0]]
没有。
断言消息清楚地表明有问题:
assert map[gString] // Fails
| ||
| |contents
| null
[contents:true]
这与 Why groovy does not see some values in dictionary? 不是同一个问题 那里的第一个答案表明:
You're adding GString instances as keys in your map, then searching for them using String instances.
在这个问题中,我清楚地添加了 GString
并尝试检索 GString
。
也不Why are there different behaviors for the ways of addressing GString keys in maps?也不Groovy different results on using equals() and == on a GStringImpl给我一个答案。我不改变任何东西,也不将 String
与 GString
混合使用。 Groovy documentation也没有帮助。
最佳答案
tl;dr:您似乎发现了 Groovy 的运行时参数重载求值中的错误。
回答:
map[gString]
被评估为 map.getAt(gString)
在运行时直接通过 Groovy 的运算符重载机制。到目前为止,一切都很好,但现在一切都开始出错了。 Java LinkedHashMap
类没有 getAt
方法在其类型层次结构中的任何位置,因此 Groovy 必须使用动态关联的 mixin 方法来代替(实际上,该语句有点颠倒。Groovy 使用 mixin 方法 before 使用类层次结构中声明的方法。)
所以,长话短说,Groovy 解析了 map.getAt(gString)
使用类别方法 DefaultGroovyMethods.getAt()
.简单易行,对吧?除了此方法有大量不同的参数重载,其中一些可能适用,尤其是当您考虑 Groovy 的默认参数强制时。
不幸的是,没有选择 DefaultGroovyMethods.getAt(Map<K,V>,K)
,这似乎是一个完美的匹配,Groovy 选择了 DefaultGroovyMethods.getAt(Object,String)
, 它强制 GString
String
中的关键参数.由于实际 key 实际上是 GString
,该方法最终无法找到该值。
对我来说,真正的 killer 是,如果直接从代码执行参数重载解析(而不是在运算符解析和类别方法选择之后),那么 Groovy 会做出正确的重载选择!也就是说,如果你替换这个表达式:
map[gString]
用这个表达式:
DefaultGroovyMethods.getAt(map,gString)
然后参数重载被正确地解析,并找到并返回正确的值。
关于dictionary - 为什么我无法获得与在 Groovy 中映射的完全相同的 GString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39145121/