Non exhaustive when
statements on sealed class/interface will be prohibited in Kotlin 1.7.
I have a sealed class State
and it's children:
sealed class State {
object Initializing : State()
object Connecting : State()
object Disconnecting : State()
object FailedToConnect : State()
object Disconnected : State()
object Ready : State()
}
In some cases I want to handle only particular items, not all, for example:
val state: State = ... // initialize
when (state) {
State.Ready -> { ... }
State.Disconnected -> { ... }
}
But I get a warning (in Kotlin 1.7 I guess it will be an error), saying:
Non exhaustive 'when' statements on sealed class/interface will be prohibited in 1.7, add 'Connecting', 'Disconnecting', 'FailedToConnect', 'Initializing' branches or 'else' branch instead
Is it a good practice to use empty else -> {}
branch here like in the next code?
when (state) {
State.Ready -> { ... }
State.Disconnected -> { ... }
else -> {}
}
Or need to add every item with an empty brackets like in the following code?
when (state) {
State.Ready -> { ... }
State.Disconnected -> { ... }
State.Connecting,
State.Disconnecting,
State.FailedToConnect,
State.Initializing -> {}
}