dimanche 25 octobre 2020

What's the correct way to iterate through properties of a singleton in Kotlin?

As the title suggests, I wanted to iterate through properties of a singleton object. I tried using the kotlin-reflect as there was no other way I found currently.

object Test {
    const val a = "String"
    const val b = "Another String"
}

Test::class.declaredMemberProperties.forEach {
    println("${it.name}: ${it.get(Test)}")
}

But unfortunately this results in the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Callable expects 0 arguments, but 1 were provided.
...
at com.example.MainKt.main(main.kt:25)  // same line as println statement
...

It seems like get() function has problem (name property resolves just fine). Is there a better approach (maybe without reflection) or some solution to access those pre-compiled constants in the singleton?





Aucun commentaire:

Enregistrer un commentaire