guenther
05/05/2018, 5:46 PMclass
with an extension property
using delegation. Of course only, when the delegated object (in this case the map) is accessible.
class Foo(val map: MutableMap<String, String> = mutableMapOf()) {
var bar1: String by map
}
var Foo.bar2: String by map
orangy
map
per Foo
instance. In your example it is not obvious why it is needed, but consider:
val Foo.bar2: String by map.filter { it.key.isNotEmpty() }
i.e. when expression is not so simpleguenther
05/05/2018, 8:59 PMUnresolved reference: map
. Also for the extension property you suggested.guenther
05/05/2018, 9:41 PMvar Foo.bar3: String
get() = map[Foo::bar3.name] as String
set(value) { map[Foo::bar3.name] = value }
But as long as the map
is public accessible, I guess there is no reason why the the extension property can't support that, is it?orangy
this.map
it will highlight this
with a proper error. But in general, when something “can obviously work” or “there is no reason why it couldn’t” it is quite often a lot of issues arising as soon as you start to actually design a language feature.orangy
guenther
05/06/2018, 5:16 PM