I have a `sealed interface MyInterface` where ever...
# getting-started
y
I have a
sealed interface MyInterface
where every implementor is
MySealedClass()
(why? because
MySealedClass
is a huge class and I need a subset of the subclasses). is there any way to make Kotlin understand this property? the best I can think of is to give the interface an infallible
fun toMySealedClass(): MySealedClass = when (this) { is MySealedClass -> this }
function which statically validates this property, but ideally I'd like not having to keep using it every time I have something that is
MyInterface
and instead have Kotlin understand that it's a
MySealedClass
.
y
Does MySealedClass need to be a class? Maybe you can extract an interface out of it, and then have MyInterface implement that
y
yes,
MySealedClass
is a major, already-existing class. it is unlikely that I'll be allowed to significantly change it.
l
Currently not possible.
Maybe some time later the compiler can infer that there's only one subtype for
MyInterface
and automatically cast that into
MySealedClass
, but it may also break the code too bad if even later another subtype for
MyInterface
is needed, as known as "extensibility hurts".
y
I see, that's good to know