Hello, how can I access the property name in a del...
# announcements
m
Hello, how can I access the property name in a delegate? I have
val foo by MyDelegate()
, I want to get the string "foo" in the
init
block of
MyDelegate
. How would I go about that?
👍 1
a
Normally you get a reference to the property (and its name) as part of the get and set calls, which allows the delegate object itself to be more stateless. If you need to know it at construction time you can use a delegate provider instead, which can then often be the stateless/reusable/constant object. https://kotlinlang.org/docs/reference/delegated-properties.html
1
m
I got it working with a delegate provider, thank you!
m
This is a very cool feature I didn’t know about! Thanks @Adam Powell