Delegating to a property of a property in Kotlin

Viewed 30

Let's consider the following code :

data class Location(var x:Int,var y:Int)
data class Player(val location:Location)

var Player.x: Int
    get() = location.x
    set(value) { location.x = value}

Is there a way to shorten this declaration using property delegates, in the style of var Player.x by instead of of the last three lines ?

1 Answers

The obvious way works, using a property reference:

data class Location(var x: Int, var y: Int)

data class Player(val location: Location) {
    var x by location::x
}
Related