2

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.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
  • I can't reproduce the warning. I would check the versions of the Kotlin plugin and Kotlin version used in your project. Maybe there's a bug that was fixed and your IntelliJ IDE has the updated plugin or that project is using a later version of Kotlin. – Tenfour04 Apr 14 '21 at 16:13
  • Thanks for checking @Tenfour04, I actually have `1.4.32-release-Studio4.1-1` for Android Studio and `1.3.72-release-IJ2020.1-5`. Do you have the same Android Studio plugin? – Henry Twist Apr 14 '21 at 16:36
  • I used Kotlin version 1.4.21 to check it. – Tenfour04 Apr 14 '21 at 18:35
  • Bizarre, I tried a new project with 1.4.21 and I still got the same warning. – Henry Twist Apr 14 '21 at 21:59
  • Same thing is happening on our side, but Kotlin compiler actually does compile the project. Must be IDEA plugin bug. – EpicPandaForce Apr 29 '21 at 12:13
  • Yes mine compiles fine too @EpicPandaForce, it's just a warning. I'm glad someone else has it, I was wondering whether it was to do with the way I designed my class structure but it's probably just a bug like you said. – Henry Twist Apr 29 '21 at 12:47
  • 1
    Experiencing the same bug with `ArcticFox 2020.3.1 Patch 3` and Kotlin plugin `203-1.5.31-release-550-AS7717.8`. It becomes annoying only with lint.isWarningsAsErrors = true. – Евгений Некрасов Nov 15 '21 at 00:56

1 Answers1

1

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.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44