class Delegate(val x: Int)
class A {
val delegate: Delegate
val x by this::delegate.x // how to do this?
}
a
Adam S
04/21/2023, 2:28 PM
try this:
Copy code
class Delegate(val x: Int)
class A(
val delegate: Delegate
) {
val x by this.delegate::x
}
👍 1
j
Joffrey
04/21/2023, 2:37 PM
Why not simply:
Copy code
class Delegate(val x: Int)
class A {
val delegate: Delegate
val x: Int get() = delegate.x
}
I find this clearer to read (but that's personal)
d
David Kubecka
04/21/2023, 2:39 PM
yeah, for
val
s I would prefer that as well probably
j
Joffrey
04/21/2023, 2:41 PM
Also, if you want
x
to be considered the same way in
Delegate
and in
A
you might want to define an interface for it (especially if you have more than one property). And then you can automatically delegate via interface delegation
➕ 1
d
David Kubecka
04/21/2023, 2:47 PM
In my current case I need to delegate just a single property so I don't think interface delegation would help me.