Identity equality for arguments of types Int and I

2019-06-17 16:13发布

问题:

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

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

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)?

回答1:

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.

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