We can can make an equality check instead of a type check for objects inside when expressions, e.g.:
sealed interface Test {
object A : Test
class B(f: Int) : Test
}
...
...
when (x) {
Test.A -> ...
is Test.B -> ...
}
But it would really look better if all branches inside when
would use type check (if one of them is a class):
when (x) {
is Test.A -> ...
is Test.B -> ...
}
As I can see in bytecode, the equality check leads us to Intrinsics.areEqual
→ and it leads us to the object’s generated equals()
→ and it makes instanceOf
(among other generated operations).
Type check also invokes same instanceOf
operation.
My question is: is it a good practice to use type checks on all branches inside when
? Are there any disadvantages?