, 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.
Copy code
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 AM
The only thing I can think of is to simplify overload resolution, or just as a way to outright disallow delegation overloads.
Unless there's a type argument variance case I'm not thinking of. But that's what I'm getting at with the second part of the question
w
Wout Werkman
07/31/2023, 8:38 AM
The property could technically be a supertype
Copy code
fun 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.