不推荐使用类型为Int和Int的参数的身份相等性 [英] Identity equality for arguments of types Int and Int is deprecated

查看:667
本文介绍了不推荐使用类型为Int和Int的参数的身份相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅供参考,这是我关于StackOverflow的第一个问题,我真的是Kotlin的新人.

Just fyi, this is my first question on StackOverflow and I'm really new in Kotlin.

在处理完全是Kotlin(版本1.1.3-2)的项目时,我在以下代码上看到警告(带有对您好奇的家伙的注释):

While working on a project that's fully Kotlin (ver 1.1.3-2), I see a warning on the following code (with the comments for you curious lads):

    // 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
            }
        }

该警告是针对可见性和View.SYSTEM_UI_FLAG_FULLSCREEN === 0 的,它说不赞成使用Int和Int类型的参数的身份相等.

The warning is for visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0, and it says Identity equality for arguments of types Int and Int is deprecated.

我应该如何更改代码?为什么首先不建议使用(出于知识的考虑)?

How should I change the code and why was it deprecated in the first place (for knowledge's sake)?

推荐答案

您可以使用结构相等,如下所示:

You can change the code by using structual equality instead as below:

//              use structual equality instead ---v
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
    decorView.systemUiVisibility = flags
}

为什么不建议使用引用相等?您可以在此处.

Why don't suggest to use referential equality? you can see my answer here.

另一方面,当您使用参照/身份相等时可能返回false,例如:

On the other hand, when you use referential/identity equality maybe return false, for example:

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和Int的参数的身份相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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