https://kotlinlang.org logo
#compiler
Title
# compiler
r

reactormonk

08/04/2022, 3:49 PM
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

dmitriy.novozhilov

08/04/2022, 6:23 PM
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

reactormonk

08/05/2022, 7:54 AM
Is there a way to make an exhaustive check for a product of sealed classes?
d

dmitriy.novozhilov

08/05/2022, 7:57 AM
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

reactormonk

08/05/2022, 7:57 AM
Can't push it into a single
when
statement?
d

dmitriy.novozhilov

08/05/2022, 7:57 AM
No
r

reactormonk

08/05/2022, 1:39 PM
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

dmitriy.novozhilov

08/05/2022, 1:47 PM
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

reactormonk

08/05/2022, 1:53 PM
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

dmitriy.novozhilov

08/08/2022, 8:09 AM
Please report issue about it to YT
d

dmitriy.novozhilov

08/08/2022, 9:56 AM
Looks similar
12 Views