Hello,
Is it possible to delegate properties to a class other than a Map?
The following example works perfectly for me.
class HostConfig(props: Map<String, Any?>) {
val ip: String by props
val port: Int by props
}
`
However when I want to delegate the props to a custom class such as:
class MyDelegate {
}
class OtherHostConfig(otherProps:MyDelegate) {
val ip:String by otherProps
val port:Int by otherProps
}
It allows me to implement getValue either for Int or String but not for both.
operator fun getValue(otherHostConfig: Any, property: KProperty<*>): Int {}
What I want to do it something like ( of course this is real hypothetic) :
fun getValue(key: String): Any? {
if ("ip".equals(key)) {
return "127.0.0.1"
} else if ("port".equals(key)) {
return 8080
} else {
return null
}
}
Is it possible to implement such a custom prop delegate for multiple fields, or is this something implemented specifically for Map class?
Thanks.