How to obtain all subclasses of a given sealed class?

后端 未结 4 1714
孤街浪徒
孤街浪徒 2021-02-02 05:56

Recently we upgraded one of our enum class to sealed class with objects as sub-classes so we can make another tier of abstraction to simplify code. However we c

4条回答
  •  庸人自扰
    2021-02-02 06:56

    With Kotlin 1.3+ you can use reflection to list all sealed sub-classes without having to use nested classes: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/sealed-subclasses.html

    I asked for some feature to achieve the same without reflection: https://discuss.kotlinlang.org/t/list-of-sealed-class-objects/10087

提交回复
热议问题