为什么groovy map关键评估在使用GStrings的类似情况下表现不同? [英] Why does groovy map key evaluation behave differently in similar circumstances when using GStrings?

查看:49
本文介绍了为什么groovy map关键评估在使用GStrings的类似情况下表现不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def t =test
我很感谢有人向我解释为什么会出现下列情况: assert [test:1] == [test:1] // 1. expected
assert [$ t:1]!= [test:1] // 2. unexpected>
assert [$ t:1]!= [test:1] // 3.意外的
assert [$ t:1] == [$ t:1] // 4. expected
println [$ t:1] //输出:[test:1]
println [test:1] //输出:[test:1]

我不明白为什么结果#2和#3有不等式。



我写了一个测试,在代码中动态创建密钥时遇到了这个问题,但考虑到测试条件,我知道它应该是字符串test。问题是返回的出现是正确的,但并不相同。我不明白为什么。



此外,以下作品:

  def t =test
def odd = [$ t:1]
assert!odd [$ t]
assert!odd.test
assert!odd [test]
assert!odd。$ t
println odd //输出:[test:1]

def d = new Date ()
def t2 =$ t
def odd2 = [(t2):1,(d):2]
assert odd2 [d] == 2
assert !odd2 [d.toString()]
assert!odd2 [t2] //预计1
odd2.put(t2,3)
println odd2 //输出:[test:3, / * date#toString * /:2]
assert odd.getAt(d)== 2
assert!odd2.getAt(t2)// expected 3


解决方案

添加这两行

  assert$ t.class.simpleName =='GStringImpl'
assert t.class.simpleName =='String'

或者只是

  println$ t.class 
println t.class

after t他的第一句话,你将能够理解为什么。 :)



如果你真的想要使用 t 的值,那么你应该使用as:

  assert [(t):1] == [test:1] //使用(t)将变量值用作键
assert [(t):1] == [test:1]
assert [(t):1]!= [$ t:1]

更新

 <$ c $因此鼓励使用(t)而不是GStringImpl 
def odd = [($ t.toString()):1]

声明奇数[$ t]
assert odd.test
assert odd [test]
声明奇数。$ t

//通过引用和按照Groovy中的值
assert$ t==test// Value Equality ==在Groovy中重写
assert!$ t.is(test)// Object Reference equal equivalent to Java in Java


I would appreciate someone explaining to me why the following is true:

def t = "test"
assert [test: 1] == ["test": 1]  // 1. expected
assert ["$t": 1] != ["test": 1]  // 2. unexpected
assert ["$t": 1] != [test: 1]    // 3. unexpected
assert ["$t": 1] == ["$t": 1]    // 4. expected
println ["$t": 1]                // output: [test: 1]
println ["test": 1]              // output: [test: 1]

I don't understand why there is the inequality for results #2 and #3.

I ran into this in writing a test where the key get dynamically created in the code, but given the test conditions I know it should be the string "test". The problem is that the returned "appears" correct but is not considered equal. And I don't understand why.

Further, the following "works":

def t = "test"
def odd = ["$t": 1]
assert !odd["$t"]
assert !odd.test
assert !odd["test"]
assert !odd."$t"
println odd           // output: [test: 1]

def d = new Date()
def t2 = "$t"
def odd2 = [(t2): 1, (d): 2]
assert odd2[d] == 2
assert !odd2[d.toString()]
assert !odd2[t2]        // expected 1
odd2.put(t2, 3)
println odd2            // output: [test: 3, /* date#toString */: 2]
assert odd.getAt(d) == 2
assert !odd2.getAt(t2)  // expected 3

解决方案

Add these 2 lines

assert "$t".class.simpleName == 'GStringImpl'
assert t.class.simpleName == 'String'

or just

println "$t".class
println t.class

after the first line, you will be able to understand why. :)

If you actually want to use the value of t then you should use as:

assert [(t): 1] == ["test": 1] //use (t) to use the variable value as key
assert [(t): 1] == [test: 1]
assert [(t): 1] != ["$t": 1]

UPDATE

//String key as before, hence encouraged to use (t) instead of GStringImpl
def odd = [("$t".toString()): 1]

assert odd["$t"]
assert odd.test
assert odd["test"]
assert odd."$t"

//Equality by reference and by value in Groovy
assert "$t" == "test" //Value Equality == overridden in Groovy
assert !"$t".is("test") //Object Reference equality equivalent to == in Java

这篇关于为什么groovy map关键评估在使用GStrings的类似情况下表现不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆