Ben Woodworth
07/31/2023, 1:37 AMoperator fun getValue
needs to take a KProperty<*>
, and can't take a more specific property type? (and same for setValue
)
And similarly, is it always safe to cast property
to a KProperty of the return type (assuming it's called as a delegate, and not directly), e.g.
operator fun Foo.getValue(thisRef: Any?, property: KProperty<*>): String {
@Suppress("UNCHECKED_CAST")
val stringProperty = property as KProperty<String>
// ...
}
Ben Woodworth
07/31/2023, 1:48 AMWout Werkman
07/31/2023, 8:38 AMfun main() {
val anyField: Any by Foo("foo")
println(anyField) // kotlin.Any
}
class Foo(val a: String)
operator fun Foo.getValue(self: Any?, property: KProperty<*>): String {
return property.returnType.toString()
}
Even though semantically, yes. I can not imagine a scenario where it breaks your expectation.