Nino
02/08/2023, 2:38 PMsealed class Foo : Bar() {
object A : Foo()
}
sealed class Bar {
object B: Bar()
}
it's totally OK to write this code :
val foo: Foo = getFoo()
val baz = when (foo) {
A -> Unit
}
I would expect B
to be required in the branchesephemient
02/08/2023, 2:39 PMB
is not a Foo
Sam
02/08/2023, 2:39 PMBar -> Foo -> A
, right?Nino
02/08/2023, 2:40 PMB
branch is needed only with Bar :
val bar: Bar = getBar()
val baz = when (bar) {
A,
B -> Unit
}
Why? B is not a FooB is a Bar, and Foo extends Bar, so... here comes my confusion ^^
Sam
02/08/2023, 2:41 PMephemient
02/08/2023, 2:41 PMBar
`- B
`- Foo
`- A
Nino
02/08/2023, 2:50 PMsealed class ASpecific : Common() {
object A : ASpecific()
}
sealed class BSpecific : Common() {
object B : BSpecific()
}
sealed class Common {
object C: Common()
}
class ScreenA {
fun run(a : ASpecific) {
when (a) {
A -> TODO()
// I would love to be able to treat 'C' there too
}
}
}
class ScreenB {
fun run(b : BSpecific) {
when (b) {
B -> TODO()
// I would love to be able to treat 'C' there too
}
}
}
Is it possible to have a "common ancestor" between 2 sealed classes so the "common ancestor" branches should be treated as well as "child" branches ?ephemient
02/08/2023, 2:52 PMsealed
sealed interface Common
sealed interface ASpecific : Common
sealed interface BSpecific : Common
object A : CommonWithA
object B : CommonWithB
object C : CommonWithA, CommonWithB
then both A is ASpecific
&& C is ASpecific
(but that naming is rather bad)Nino
02/08/2023, 3:02 PMSealedClassForScreenA
and SealedClassForScreenB
that would makes such a code work ?
class ScreenA {
fun run(a: SealedClassForScreenA) {
when (a) {
A -> TODO()
C -> TODO()
}
}
}
class ScreenB {
fun run(b: SealedClassForScreenB) {
when (b) {
B -> TODO()
C -> TODO()
}
}
}
ephemient
02/08/2023, 3:03 PMNino
02/08/2023, 3:04 PMephemient
02/08/2023, 3:04 PMNino
02/08/2023, 3:04 PM