1

Why IntDef in Kotlin is not exhaustive? The next piece of code requires me to add an else branch, but I don't see why should I. I tried to put annotation wherever I could, but no luck.

Is it possible somehow, or do I have to use sealed class for desired behavior?

@IntDef({AWAIT, FAILED, SENT, SEEN})
@Retention(AnnotationRetention.SOURCE)
annotation class Status 

companion object {
    const val AWAIT = 0;
    const val FAILED = 1;
    const val SENT = 2;
    const val SEEN = 3;
}

@Status val status = AWAIT
when (status) {   // THIS is red underlined cause it is "not" exhaustive
    AWAIT -> {}
    FAILED -> {}
    SENT -> {}
    SEEN -> {}
}
Majkeee
  • 960
  • 1
  • 8
  • 28
  • 1
    https://stackoverflow.com/a/37839539/7366707 - tl;dr: use an enum. – Salem Mar 20 '18 at 19:20
  • 1
    @Moira Hello, the linked question is not the same as this one. The linked question asks if you can use `IntDef` in Kotlin. This question is asking why `IntDef` can't be exhaustive without an else clause in a `when` block. Using an `enum` does not allow a `when` block to be considered exhaustive without an else clause. – nukeforum Aug 20 '18 at 21:24

0 Answers0