Extension delegation properties It should be poss...
# language-proposals
g
Extension delegation properties It should be possible, to extend a
class
with an
extension property
using delegation. Of course only, when the delegated object (in this case the map) is accessible.
Copy code
class Foo(val map: MutableMap<String, String> = mutableMapOf()) {
    var bar1: String by map
}
var Foo.bar2: String by map
o
It is not possible right now, because there is no storage for an extension property to keep a value of
map
per
Foo
instance. In your example it is not obvious why it is needed, but consider:
Copy code
val Foo.bar2: String by map.filter { it.key.isNotEmpty() }
i.e. when expression is not so simple
g
hm, actually the problem I get is
Unresolved reference: map
. Also for the extension property you suggested.
Workaround of course is possible with e.g.
Copy code
var 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?
o
Yes, if you write it like
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.
Filed an issue for diagnostics: https://youtrack.jetbrains.com/issue/KT-24289
g
Ok, but improving the error message wasn't quite what I expected. What I want to "discuss" is, if Extension delegation properties are useful for the language.