Alex Vanyo
03/10/2021, 4:00 AMsealed interface Alpha {
interface A : Alpha
interface B : Alpha
}
sealed class Beta /* : Alpha */ {
object Y : Beta(), Alpha.A
object Z : Beta(), Alpha.B
}
fun Beta.fix(): Alpha = when (this) {
is Beta.Y -> this
is Beta.Z -> this
}
fun first(beta: Beta) = second(beta.fix()) // Could fix() be implicit?
fun second(alpha: Alpha) = when (alpha) {
is Alpha.A -> "A"
is Alpha.B -> "B"
}
All subclasses of Beta
implement one of the subinterfaces of Alpha
, so Beta is Alpha
is always true.
However, Beta
can't directly implement Alpha
without adding more subclasses to Alpha
.
It's possible to make this explicit with something like the Beta.fix()
above, which shows that Beta is Alpha
is always true, but it'd be really cool if that could be done automatically in some way.elizarov
03/11/2021, 4:57 PMAlex Vanyo
03/11/2021, 5:34 PMobject Gamma {
fun doSomethingWithAlpha(alpha: Alpha) {
/* ... */
}
}
fun third(beta: Beta) {
Gamma.doSomethingWithAlpha(beta.fix()) // Could fix() be implicit?
}
fix()
is explicitly showing that Beta is Alpha
is always true.