47

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

Aba
  • 2,307
  • 2
  • 18
  • 32
  • 5
    for primitive types, `===` (reference equality) is the same as `==` (`equals` equality). The compiler will use reference equality for both for primitive types, so idiomatic Kotlin uses `==` for primitive types. – Eric Cochran Jul 20 '17 at 21:53
  • @EricCochran Thanks! That puts it very simply. – Aba Jul 21 '17 at 16:11

2 Answers2

45

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
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • Thanks for the explanation! So, in short, the reason that we should not be using referential equality in this case is that it may not yield the result that we expect (false when it's actually true)? – Aba Jul 21 '17 at 16:10
  • Cool. I need to read and practice a lot more. Appreciate your help. :) – Aba Jul 21 '17 at 16:18
0

For type Int the referential equality === (this is used if left and right part are pointing to the same object) is equal to ==

Refer to https://kotlinlang.org/docs/equality.html#referential-equality for more information on this.

R. Rohilla
  • 71
  • 8