Does this already exist in the stdlib? And if not...
# stdlib
r
Does this already exist in the stdlib? And if not, why is it a dreadful idea? 😃 (I find that most of the time when I use
PropertyDelegateProvider
it's just to capture the name of the property)
This is an example use case - just want to pass the property name to an arbitrary function to avoid the repetition.
y
I'm pretty sure
Map
already provides a
getValue
that does exactly that btw. But yes you can do that
r
So it does, that's cool. (Map access wasn't actually my use case, just wanted to show an easily understandable example.)
y
I think it's likely better to just define the operator for your class, but this is fine too. I would also suggest, instead of using
ReadOnlyProperty
, to instead do something like:
Copy code
@JvmInline value class Wrapper<T>(val value: T)
inline operator fun <T> Wrapper<T>.getValue(...): T = value
r
I'm not totally following, I confess. In my use case there isn't a class to add it to, I don't think.
y
Instead of returning a
ReadOnlyProperty
you can just return a
Wrapper
is what I'm saying. It's a little bit of reduced wrapping, that's all
r
Like this?
1
Bit ugly that I have to import
getValue
as well as
passPropertyNameTo
- but I guess that's the price of the inlining... I think I'll take the hit of the extra object allocations and go with this:
y
You could probably just make the extension a member instead and that's it