I've got two sealed classes `CurrentState` and `De...
# compiler
r
I've got two sealed classes
CurrentState
and
DesiredState
. When doing
val x = when (CurrentState to DesiredState) { ... }
, the exhaustiveness checker doesn't seem to like the non-existence of an
else
, even if I covered all branches. Any way to help it out a bit?
d
Exhautive checker does not work this way, because Pair of to sealed classes is not exhaustive class BTW how do you check for pair components are is instance of some inheritor?
r
Is there a way to make an exhaustive check for a product of sealed classes?
d
Copy code
sealed class A
class B : A()
class C : A()

sealed class X
class Y : X()
class Z : X()

fun test(a: A, x: X) {
    val result = when (a) {
        is B -> when (x) {
            is Y -> 1
            is Z -> 2
        }
        is C -> when (x) {
            is Y -> 3
            is Z -> 4
        }
    }
}
r
Can't push it into a single
when
statement?
d
No
r
Should the exhaustiveness check also work with
Copy code
sealed class DesiredState: Parcelable {
    @Parcelize
    object MainPage: DesiredState()
    @Parcelize
    object AcceptCall: DesiredState()
}
?
Currently tells me I also need an
else
branch
d
Copy code
sealed class DesiredState: Parcelable {
    @Parcelize
    object MainPage: DesiredState()
    @Parcelize
    object AcceptCall: DesiredState()
}

fun test(state: DesiredState) = when (state) {
    is DesiredState.MainPage -> "OK"
    is DesiredState.AcceptCall -> "Error"
}
Works fine
r
Looks like intellij got stuck somewhere, works fine.
I've got a weird case here where Intellij tells me I need an
else
branch, but the compiler is fine with it 🤔
d
Please report issue about it to YT
d
Looks similar