Kotlin identity Equals 没有按预期工作
Kotlin's identityEquals not working as expected
根据 kotlin reference,此代码片段应打印 "true"
val a: Int = 10000
print(a identityEquals a)
但是当我尝试它时(版本是 0.12.1218),它打印 "false"。为什么?
2015/07/29 更新
如果让 a 在 [-128 ~ 127] 中,它会打印 "true"。正如安德烈所说,应该使用 ===
.
我找到了来自 Andrey Breslav 的 explain。
The reason is that under the hoods Int is not a reference type: it's represented by a primitive int, but when you are calling identityEquals, who signature is Any?.identityEquals(Any?), it both operands get boxed, and as a result we have two different objects. We'll fix this by introducing overloads of identityEquals for Java primitives.
到现在还没解决。官方参考资料应该解释这一点。困扰了我好几个小时
identityEquals
即将弃用,请使用 ===
根据 kotlin reference,此代码片段应打印 "true"
val a: Int = 10000
print(a identityEquals a)
但是当我尝试它时(版本是 0.12.1218),它打印 "false"。为什么?
2015/07/29 更新
如果让 a 在 [-128 ~ 127] 中,它会打印 "true"。正如安德烈所说,应该使用 ===
.
我找到了来自 Andrey Breslav 的 explain。
The reason is that under the hoods Int is not a reference type: it's represented by a primitive int, but when you are calling identityEquals, who signature is Any?.identityEquals(Any?), it both operands get boxed, and as a result we have two different objects. We'll fix this by introducing overloads of identityEquals for Java primitives.
到现在还没解决。官方参考资料应该解释这一点。困扰了我好几个小时
identityEquals
即将弃用,请使用 ===