Sealed Object Instances Status

A Kotlin Symbol Processor to list sealed object instances.


Let’s say you have a similar structure of sealed classes (or interfaces):

// FeatureFlag.kt
sealed class FeatureFlag {
    abstract val isEnabled: Boolean

// Debug.kt
sealed class Debug(override val isEnabled: Boolean = false): FeatureFlag() {
    object Logs: Debug(true)
    object Traces: Debug()
    object StrictMode: Debug()

// UI.kt
sealed class UI(override val isEnabled: Boolean = false): FeatureFlag() {
    object Animations: UI()
    object Framerate: UI()

And you want to automatically list all FeatureFlags, then you have at least 3 options:

  • ? Manually keep a list of objects somewhere, but this is cumbersome and error-prone.
  • ? Use Kotlin reflection to browse the class declaration, with the following extension FeatureFlag::class.reflectSealedObjectInstances(), but this can become an expensive operation if you have a lot of objects and a deeply nested structure.
  • ? Use Kotlin Symbol Processing (KSP) to create the list of objects at compile time.

This is exactly what this project does. You simply need to add the SealedObjectInstances annotation on the sealed class:

sealed class FeatureFlag { /*...*/ }

And then you’ll have access to the sealedObjectInstances() extension on the corresponding type:

val flags: Set<FeatureFlag> = FeatureFlag::class.sealedObjectInstances()


In the module’s build script, apply the plugin with the current Kotlin version and add this library to the list of dependencies.

plugins {
    id("") version "1.7.10-1.0.6"

repositories {
    maven {
        url = uri("")

dependencies {

Make IDE aware of generated code

By default, IntelliJ IDEA or other IDEs don’t know about the generated code. So it will mark references to generated symbols unresolvable. To make an IDE be able to reason about the generated symbols, mark the following paths as generated source roots:

kotlin {
    sourceSets.main {
    sourceSets.test {


View Github