Yevhenii Nadtochii
05/07/2020, 10:36 PMoperator fun getValue(thisRef: Any, property: KProperty<*>): Any = properties[property.name]
?: throw IllegalStateException("Property ${property.name} hasn't been initialized!")
operator fun setValue(thisRef: Any, property: KProperty<*>, value: Any) {
properties[property.name] = value
listeners[property.name]?.invoke(value.toString())
}
dmitriy.novozhilov
05/08/2020, 9:00 AMAny
as return type of set
and value
type of get
you can use SharedStats
only for properties of type Any
- you try to declare delegate for property with no receiver, so your set
and get
should take Nothing?
as thisRef
, not Any
(since top-level properties has no receiver at all)
Correct declaration should look like this:
class SharedStatsFixed {
operator fun <T> getValue(thisRef: Nothing?, property: KProperty<*>): T = TODO()
operator fun <T> setValue(thisRef: Nothing?, property: KProperty<*>, value: T) {
TODO()
}
}
For more details check reference: https://kotlinlang.org/docs/reference/delegated-properties.html#property-delegate-requirements forYevhenii Nadtochii
05/08/2020, 12:47 PMdmitriy.novozhilov
05/08/2020, 12:52 PM