https://kotlinlang.org logo
#announcements
Title
# announcements
n

nkiesel

10/20/2017, 9:32 PM
I would like to delegate properties to other properties instead of providing getter and setter, is that possible? Perhaps using
val x by y.z
syntax? A concrete example is the following
Copy code
interface A {
    val a: String
}

class AImpl : A {
    override val a = "hello"
}

interface B {
    var b: Int
}

class BImpl : B {
    override var b = 42
}

class C : A, B {
    private val _a = AImpl()
    private val _b = BImpl()

    // override val a by _a.a
    override val a
        get() = _a.a

    // override val b by _b.b
    override var b
        get() = _b.b
        set(v) { _b.b = v }
}

fun main(args: Array<String>) {
    val c = C()
    c.b = 11
    println("a=${c.a} b=${c.b}")
}
k

karelpeeters

10/20/2017, 10:07 PM
There's a YouTrack issue for this feature request: https://youtrack.jetbrains.com/issue/KT-8658
2 Views