Noob question about sealed classes usage, what am ...
# getting-started
j
Noob question about sealed classes usage, what am I missing here
image.png
t
it should be
object Bar2 : Reason()
j
but then its not sub-type BarReason right?
r
Nested
object
isn't a subtype of the thing it's nested in.
Bar1
has type
BarReason.Bar1
, which is not a subtype of
BarReason
.
t
it is not a sub class of BarReason in your example though, is it?
val reason: BarReason = BarReason.Bar1
fails righ?
sniped 😄
😁 1
j
Yeah that is correct
v
So make it
object Bar2 : BarReason()
j
Yeah I think I got it
Copy code
sealed class Reason
sealed class FooReason: Reason()
sealed class BarReason: Reason()

object Bar1: BarReason()

fun reason(reason: Reason) {
    when (reason) {
        is FooReason -> println("Foo reason" + reason)
        is BarReason -> println("Bar reason" + reason)
    }
}

class ReasonTest {
    fun test() {
        reason(Bar1)
    }
}
and when is exhaustive
👍 1