https://kotlinlang.org logo
#getting-started
Title
# getting-started
g

GUIGAL Allan

09/03/2021, 4:49 PM
Is it possible to have a reified T for this operator fun ? Or to know the T class at runtime ?
Copy code
operator fun getValue(thisRef: Any?, property: KProperty<T>): T {
e

ephemient

09/03/2021, 4:59 PM
an extension fun should work
g

GUIGAL Allan

09/03/2021, 5:01 PM
I’m in a delegated property, can you make extension fun with the operator keyword ?
e

ephemient

09/03/2021, 5:03 PM
yes
g

GUIGAL Allan

09/03/2021, 5:04 PM
What would you extend ? Thanks for the idea but I’m not sure how to put it
e

ephemient

09/03/2021, 5:07 PM
an example: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/get-value.html which allows you to write
val foo by ::bar
because
::bar
is a
KProperty
that is the receiver
g

GUIGAL Allan

09/03/2021, 5:11 PM
I see, I give a whole class tho “var foo: MyObject by MongoField” with a couple more operators inside and it looks like I can’t have the extension fun one inside
“Property delegate must have a ‘setValue(User, KProperty*>, *)’ method. None of the following functions are suitable.”
t

turansky

09/03/2021, 6:41 PM
Also you can create delegate in
inline
method
Copy code
val a by myDelegate<String>()

inline fun <reified ...> myDelegate(): ReadOnlyProperty<..., T> = MyDelegate(T::class)
🙌 1
1
🙌 1
1
4 Views