In the topic bellow, property.name is introduced as compile-time constant, but this does not work correctly with inline functions
Example (simplified, not makes sense much):
import kotlin.reflect.KProperty
class WrapperClass(private val data: Map<String, Any?>) {
val value1: Int
get() = data.getTyped(WrapperClass::value1)
// not ok, creates extra class which extends PropertyReference1Impl for the property is generated
val value2: Int
get() = data.getTyped(WrapperClass::value2.name)
// ok, same as:
// get() = data.getTyped("value2")
}
inline fun <T> Map<String, Any?>.getTyped(name: String): T {
return this[name] as T
}
inline fun <T> Map<String, Any?>.getTyped(prop: KProperty<*>): T {
return getTyped(prop.name)
}
I expect, that this:
get() = data.getTyped(WrapperClass::value1)
to generate equally efficient code as this
get() = data.getTyped(“value1”)
- reason why I use property instead of its name directly is that It holds its type, but property.name does not