不推荐使用 Int 和 Int 类型参数的身份相等性
Identity equality for arguments of types Int and Int is deprecated
仅供参考,这是我在 Whosebug 上的第一个问题,我是 Kotlin 的新手。
在处理一个完全使用 Kotlin(版本 1.1.3-2)的项目时,我在以下代码中看到一条警告(为好奇的小伙子们提供评论):
// Code below is to handle presses of Volume up or Volume down.
// Without this, after pressing volume buttons, the navigation bar will
// show up and won't hide
val decorView = window.decorView
decorView
.setOnSystemUiVisibilityChangeListener { visibility ->
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) {
decorView.systemUiVisibility = flags
}
}
警告是针对 visibility 和 View.SYSTEM_UI_FLAG_FULLSCREEN === 0,它说 Identity equality for arguments of types Int and Int is deprecated .
我应该如何更改代码以及为什么它首先被弃用(为了知识)?
您可以使用 structual equality 更改代码,如下所示:
// use structual equality instead ---v
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
decorView.systemUiVisibility = flags
}
为什么不建议使用referential equality? you can see my answer 。
另一方面,当您使用 referential/identity equality 时可能 return false
,例如:
val ranged = arrayListOf(127, 127)
println(ranged[0] === ranged[1]) // true
println(ranged[0] == ranged[1]) // true
val exclusive = arrayListOf(128, 128)
// v--- print `false` here
println(exclusive[0] === exclusive[1]) // false
println(exclusive[0] == exclusive[1]) // true
对于类型 Int
,引用相等性 ===
(如果左右部分指向同一对象,则使用此方法)等于 ==
有关这方面的更多信息,请参阅 https://kotlinlang.org/docs/equality.html#referential-equality。
仅供参考,这是我在 Whosebug 上的第一个问题,我是 Kotlin 的新手。
在处理一个完全使用 Kotlin(版本 1.1.3-2)的项目时,我在以下代码中看到一条警告(为好奇的小伙子们提供评论):
// Code below is to handle presses of Volume up or Volume down.
// Without this, after pressing volume buttons, the navigation bar will
// show up and won't hide
val decorView = window.decorView
decorView
.setOnSystemUiVisibilityChangeListener { visibility ->
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) {
decorView.systemUiVisibility = flags
}
}
警告是针对 visibility 和 View.SYSTEM_UI_FLAG_FULLSCREEN === 0,它说 Identity equality for arguments of types Int and Int is deprecated .
我应该如何更改代码以及为什么它首先被弃用(为了知识)?
您可以使用 structual equality 更改代码,如下所示:
// use structual equality instead ---v
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
decorView.systemUiVisibility = flags
}
为什么不建议使用referential equality? you can see my answer
另一方面,当您使用 referential/identity equality 时可能 return false
,例如:
val ranged = arrayListOf(127, 127)
println(ranged[0] === ranged[1]) // true
println(ranged[0] == ranged[1]) // true
val exclusive = arrayListOf(128, 128)
// v--- print `false` here
println(exclusive[0] === exclusive[1]) // false
println(exclusive[0] == exclusive[1]) // true
对于类型 Int
,引用相等性 ===
(如果左右部分指向同一对象,则使用此方法)等于 ==
有关这方面的更多信息,请参阅 https://kotlinlang.org/docs/equality.html#referential-equality。