https://kotlinlang.org logo
#reflect
Title
# reflect
d

Daniele B

08/13/2020, 5:27 PM
Copy code
var myVar : String
    get() = functionUsingTheVariableName(::field.name)
I would like to do this, but I get:
Unsupported [References to variables aren't supported yet]
any alternative?
e

ephemient

08/13/2020, 6:46 PM
why not
functionUsingTheVariableName(::myVar.name)
?
else you could probably get super fancy with
Copy code
operator fun <T> ((String) -> T).getValue(thisRef: Any?, property: KProperty<*>): T = invoke(property.name)

val myVar: String by ::functionUsingTheVariableName
d

Daniele B

08/13/2020, 6:48 PM
the whole point is was not to repeat the name of the variable
but yes, I looked into the Delegated Properties and I also found it suits the best!
thanks
2 Views