Hi, i’m trying out a construct like this:
sealed class UIEvent {
class UsernameEvent : UIEvent()
sealed class PasswordEvent : UIEvent() {
class OnChangeText : PasswordEvent()
class OnBlur : PasswordEvent()
}
}
I tried handling it like this, but I get an error that PasswordEvent
isn’t handled.
fun handle(ev: UIEvent): String = when (ev) {
is UIEvent.UsernameEvent -> ""
is UIEvent.PasswordEvent.OnChangeText -> ""
is UIEvent.PasswordEvent.OnBlur -> ""
}
This would also be nice, but the inner when
isn’t casted, and it complains about Username
Event not being handled:
fun handle(ev: UIEvent): String = when (ev) {
is UIEvent.UsernameEvent -> "a"
is UIEvent.PasswordEvent -> when(ev) {
is UIEvent.PasswordEvent.OnBlur -> "b"
is UIEvent.PasswordEvent.OnChangeText -> "c"
}
}
A follow-up question: Why do I have to manually import the sealed classes, in order to remove the prefix in the branches? This is a bit tedious especially because the IDEA plugin can’t resolve the reference automatically.
Thank you.