# \[SOLVED\]\[Request\] Put extension delegate property in receiver scope

**URL:** <https://discuss.kotlinlang.org/t/solved-request-put-extension-delegate-property-in-receiver-scope/14904>\
**Category:** Language Design\
**Created:** [November 5, 2019, 11:35pm UTC](https://discuss.kotlinlang.org/t/solved-request-put-extension-delegate-property-in-receiver-scope/14904 "2019-11-05T23:35:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [November 5, 2019, 11:35pm UTC](https://discuss.kotlinlang.org/t/solved-request-put-extension-delegate-property-in-receiver-scope/14904/1 "2019-11-05T23:35:46Z")

</div>

I think that Kotlin could benefit from putting the delegate inside of the receiver scope when extending a property.

How possible would it be to put the delegate declaration inside the scope of the receiver?

For this example, say we cannot change `Player` and the developer gave us some data, but we know nothing about the data. It would allow us to do something like this

```auto
import kotlin.reflect.KProperty

data class Player(val useraname: String, val playerId: Int, var someData: Int)

fun Int.getBits(startBit: Int, endBit: Int): Int =
    TODO("Get a value representing all bits in this inclusively between startBit and endBit")

fun Int.setBits(startBit: Int, endBit: Int, value: Int): Int =
    TODO("Return this int with all bits from startBit to endBit replaced with value's bits")

class PlayerData(val player: Player, val dataStartBit: Int, val dataEndBit: Int) {

    operator fun getValue(ref: Any?, prop: KProperty<*>): Int = player.someData.getBits(dataStartBit, dataEndBit)

    operator fun setValue(ref: Any?, prop: KProperty<*>, value: Int) {
        player.someData = player.someData.setBits(dataStartBit, dataEndBit, value)
    }
}

// this is the instance of the player
var Player.health by PlayerData(this, 0, 7)
var Player.mana by PlayerData(this, 8, 15)
var Player.stamina by PlayerData(this, 16, 23)

```

I think this is an improvement in quality of life over something like the current “solution” to this problem which is more long winded than the proposed alternative while increasing code re-usability and decreasing copy-paste.

```auto
var Player.health: Int
    get() = someData.getBits(0, 7)
    set(value) { someData.setBits(0, 7, value) }

var Player.mana: Int
    get() = someData.getBits(8, 15)
    set(value) { someData.setBits(8, 15, value) }

var Player.stamina: Int
    get() = someData.getBits(16, 23)
    set(value) { someData.setBits(16, 23, value) }
`
```

---

<div class="post-metadata">

**Author:** ![rrlynx](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rrlynx](https://discuss.kotlinlang.org/u/rrlynx)\
**Post date:** [November 6, 2019, 6:33am UTC](https://discuss.kotlinlang.org/t/solved-request-put-extension-delegate-property-in-receiver-scope/14904/2 "2019-11-06T06:33:59Z")

</div>

Is this what you want to achieve?

```auto
class PlayerData(val dataStartBit: Int, val dataEndBit: Int) {

    operator fun getValue(ref: Player, prop: KProperty<*>): Int =
        ref.someData.getBits(dataStartBit, dataEndBit)

    operator fun setValue(ref: Player, prop: KProperty<*>, value: Int) {
        ref.someData = ref.someData.setBits(dataStartBit, dataEndBit, value)
    }
}

var Player.health by PlayerData(0, 7)
var Player.mana by PlayerData(8, 15)
var Player.stamina by PlayerData(16, 23)

```

---

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [November 6, 2019, 8:30am UTC](https://discuss.kotlinlang.org/t/solved-request-put-extension-delegate-property-in-receiver-scope/14904/3 "2019-11-06T08:30:26Z")

</div>

This is actually exactly what I want to achieve. I had no idea what `ref` actually did and assumed it was just arbitrarily there.

Thank you!
