https://kotlinlang.org logo
#getting-started
Title
# getting-started
y

y

10/16/2023, 10:14 AM
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

Youssef Shoaib [MOD]

10/16/2023, 11:18 AM
Does MySealedClass need to be a class? Maybe you can extract an interface out of it, and then have MyInterface implement that
y

y

10/16/2023, 11:26 AM
yes,
MySealedClass
is a major, already-existing class. it is unlikely that I'll be allowed to significantly change it.
l

Loney Chou

10/17/2023, 7:20 AM
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

y

10/17/2023, 7:29 AM
I see, that's good to know