Question about `when` and matching an `object` fro...
# codingconventions
a
Question about
when
and matching an
object
from a
sealed
class:
Copy code
sealed class Foo {
    object Bar : Foo()
    data class Baz(val x: Int) : Foo()
}

fun usingEquality(foo: Foo) = when (foo) {
    Foo.Bar -> 1
    is Foo.Baz -> 2
}

fun usingInstanceOf(foo: Foo) = when (foo) {
    is Foo.Bar -> 1
    is Foo.Baz -> 2
}
Any reason to prefer 1️⃣
usingEquality
rather than 2️⃣
usingInstanceOf
? IDE generates
usingEquality
, but doing a quick benchmark shows no difference, yet
is Foo.Bar
is more useful, in case you decide later to make
Bar
a
data class
for example. What do you use?
1️⃣ 1
2️⃣ 5
c
I prefer (2) due to this issue: https://youtrack.jetbrains.com/issue/KT-22538