Is there an alternative to using sealedSubclasses ...
# javascript
j
Is there an alternative to using sealedSubclasses on kotlin-js (reflecting on the list of classes in a sealed class)? I've ran into this working only on the jvm a few times now. My (ugly) workaround is using some enum that just duplicates all the implementing class names just so I van iterate over those things. Is there a more elegant solution to this? I know I can do an exhaustive when on objects. But I can't just iterate over all the implementing classes. In general, it seems reflection is a bit overlooked for this. Also, given that the sealed hierarchy is known at compile time, having them behave a bit more like enums (by e.g. having entries implemented) would be nice.
a
It sounds nice. Could you please create an YouTrack ticket for it? P.S. As a more nice workaround for now you could try to write a compiler plugin that will create a property inside the sealed parent class companion that store class references on its subclasses
t
If I understand your problem correctly, this library seems like it may be helpful: https://github.com/livefront/sealed-enum. Disclaimer: I haven’t tried it myself or know if it works for multiplatform, but it may be worth looking into!
j
Trying to avoid depending on compiler plugins or hacks. PITA to support those over time IMHO. There is an existing bug for supporting this: https://youtrack.jetbrains.com/issue/KT-42007/KJS-Reflection-add-KClasssealedSubclasses
t
fwiw, the repo I linked uses #ksp which is not a compiler plugin and is generated code only. I agree there needs to be an official way, but using something like code generation may be a safer temporary solution than manually maintaining an exhaustive list.