Can I delegate to a nested property? ```class Dele...
# getting-started
d
Can I delegate to a nested property?
Copy code
class Delegate(val x: Int)

class A {
  val delegate: Delegate
  val x by this::delegate.x // how to do this?
}
a
try this:
Copy code
class Delegate(val x: Int)

class A(
  val delegate: Delegate
) {
  val x by this.delegate::x
}
👍 1
j
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
yeah, for
val
s I would prefer that as well probably
j
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
In my current case I need to delegate just a single property so I don't think interface delegation would help me.