Android Studio not recognising that a when statement is exhaustive with a sealed class

824 views Asked by At

I have a simple result class called Outcome which is sealed and consists of three subclasses: Waiting, Success and Failure:

sealed class Outcome<out T> {

    object Waiting : Outcome<Nothing>()

    class Success<T>(val data: T) : Outcome<T>()

    open class Failure : Outcome<Nothing>()
}

Now I want to use a when statement to check which type a generic Outcome is, however Android Studio (and specifically Android Studio, IntelliJ IDEA doesn't have this issue) seems to think that it is not exhaustive and gives the warning:

'when' expression on sealed classes is recommended to be exhaustive, add 'is Failure', 'is Success', 'Waiting' branches or 'else' branch

However the when definitely is exhaustive:

when (o) {

    is Outcome.Failure -> TODO()
    is Outcome.Success -> TODO()
    Outcome.Waiting -> TODO()
}

and even when I ask for the remaining branches to be added, it simply adds a copy of what I already have and still yields the same error:

when (o) {

    is Outcome.Failure -> TODO()
    is Outcome.Success -> TODO()
    Outcome.Waiting -> TODO()
    is Outcome.Failure -> TODO()
    is Outcome.Success -> TODO()
    Outcome.Waiting -> TODO()
}

This isn't a massive issue as it is only a warning and doesn't cause any actual problems. However I would like to know whether this is an inference bug or if it is something related to my design.


Note: My Android Studio plugin version is 1.4.32-release-Studio4.1-1 and I have tried this with a completely new project and got the same results.

1

There are 1 answers

0
Henry Twist On BEST ANSWER

This issue has now been rectified since upgrading to Kotlin 1.5 (specifically 202-1.5.0-release-764-AS8194.7) so it looks like it must have just been an inference bug.