https://kotlinlang.org logo
#announcements
Title
# announcements
h

hagabaka

11/01/2017, 11:33 PM
is it possible to access a property or function by string containing its name at run time, like in JavaScript?
k

karelpeeters

11/01/2017, 11:37 PM
Do you mean in Kotlin-JS? If so take a look at
js(...)
. There's also the reflection API, https://kotlinlang.org/docs/reference/reflection.html
h

hagabaka

11/01/2017, 11:38 PM
I'm developing for Android
k

karelpeeters

11/01/2017, 11:40 PM
Then reflection should work. Beware that "finding a function by name at runtime" is often a sign of bad design.
h

hagabaka

11/01/2017, 11:44 PM
how do you use reflection to do that? I see that you can get references to properties and functions, but it doesn't show a way to use a string to specify their names
k

karelpeeters

11/01/2017, 11:45 PM
You'd loop over all of the functions of a class and check for a name match.
h

hagabaka

11/01/2017, 11:46 PM
how do you get a list of functions of a class?
h

hagabaka

11/02/2017, 12:02 AM
all right, thanks!
👍 1
2 Views