Hi guys, is there a better way to express a delega...
# announcements
t
Hi guys, is there a better way to express a delegated property
var mapType
get() = map.mapType
set(value) {
map.mapType = value
}
I know we have Delegates but can those be used in a more simple way than my implementation above?
s
You’d have to implement the
setValue
and
getValue
operators for whatever type
map
is then you can use
var mapType by map
The delegate pattern where you implement the operators is really only useful if the type is delegated to a lot. For one-offs using the backing field directly like you have is perfectly fine.
t
OK, thanks. Then I already have the shortes implementation . Thought there would be something like
var mapType by proxy(map.mapType)
or even
var mapType proxies map.mapType
somehow generic. Quiet common use case when encapsulating 3rd party components.
a
You can probably write something to do
var mapType by proxy(map::mapType)
if you’re using it a lot, and then write it up as a suggestion for stdlib