# Possible Feature: Kotlin Access Property Name from Delegate

**URL:** <https://discuss.kotlinlang.org/t/possible-feature-kotlin-access-property-name-from-delegate/19597>\
**Category:** Support\
**Created:** [October 22, 2020, 6:12am UTC](https://discuss.kotlinlang.org/t/possible-feature-kotlin-access-property-name-from-delegate/19597 "2020-10-22T06:12:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonatino](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jonatino/32/8065_2.png) [@Jonatino](https://discuss.kotlinlang.org/u/Jonatino)\
**Post date:** [October 22, 2020, 6:12am UTC](https://discuss.kotlinlang.org/t/possible-feature-kotlin-access-property-name-from-delegate/19597/1 "2020-10-22T06:12:21Z")

</div>

I’m writing a saving system for my game using kotlin delegated properties.

Here is a small example

```auto
import kotlin.reflect.KProperty

object Test {
    val test: Int by no_prop { 10 } // I want this
    val testTwo: Int by prop(::testTwo) { 10 } // I don't want this if possible
}

inline fun <reified T : Any> Test.no_prop(initializer: () -> T) = TestProp<T>()

inline fun <reified T : Any> Test.prop(prop: KProperty<*>, initializer: () -> T): TestProp<T> {
    println("My saving key is ${this.javaClass.simpleName}.${prop.name}")
    return TestProp<T>()
}

class TestProp<T : Any> {
    operator fun getValue(thisRef: Any?, property: KProperty<*>) = 10 as T
    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {}
}

```

My game uses a string key for saved properties. Its always `*callerClassName*.*propertyName*`

I was wondering if its possible through my delegate extension functions or the `TestProp` class to access the name of the property is delegating?

Ex:  
It would be nice if the no\_prop method could the property that called it was named “test”

```auto
val test: Int by no_prop { 10 }

```

The following is a workaround, but I’d like to avoid doing that for every property if possible

```auto
val testTwo: Int by prop(::testTwo) { 10 }

```

Is this possible in kotlin?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [October 22, 2020, 4:07pm UTC](https://discuss.kotlinlang.org/t/possible-feature-kotlin-access-property-name-from-delegate/19597/2 "2020-10-22T16:07:44Z")

</div>

That is exactly the reasoning behind the `provideDelegate` operator. [Here’s](https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS40LjEwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsImpzQ29kZSI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiaW1wb3J0IGtvdGxpbi5yZWZsZWN0LktQcm9wZXJ0eVxuXG5vYmplY3QgVGVzdCB7XG4gICAgdmFsIHRlc3Q6IEludCBieSBub19wcm9wIHsgMTAgfSAvLyBJIHdhbnQgdGhpc1xuICAgIHZhbCB0ZXN0VHdvOiBJbnQgYnkgcHJvcCg6OnRlc3RUd28pIHsgMTAgfSAvLyBJIGRvbid0IHdhbnQgdGhpcyBpZiBwb3NzaWJsZVxufVxuXG5pbmxpbmUgZnVuIDxyZWlmaWVkIFQgOiBBbnk+IFRlc3Qubm9fcHJvcChpbml0aWFsaXplcjogKCkgLT4gVCkgPSBUZXN0UHJvcDxUPigpXG5cbmlubGluZSBmdW4gPHJlaWZpZWQgVCA6IEFueT4gVGVzdC5wcm9wKHByb3A6IEtQcm9wZXJ0eTwqPiwgaW5pdGlhbGl6ZXI6ICgpIC0+IFQpOiBUZXN0UHJvcDxUPiB7XG4gICAgcmV0dXJuIFRlc3RQcm9wPFQ+KClcbn1cblxuY2xhc3MgVGVzdFByb3A8VCA6IEFueT4ge1xuICAgIG9wZXJhdG9yIGZ1biBnZXRWYWx1ZSh0aGlzUmVmOiBBbnk/LCBwcm9wZXJ0eTogS1Byb3BlcnR5PCo+KSA9IDEwIGFzIFRcbiAgICBvcGVyYXRvciBmdW4gc2V0VmFsdWUodGhpc1JlZjogQW55PywgcHJvcGVydHk6IEtQcm9wZXJ0eTwqPiwgdmFsdWU6IFQpIHt9XG4gICAgb3BlcmF0b3IgZnVuIHByb3ZpZGVEZWxlZ2F0ZSh0aGlzUmVmOiBBbnk/LCBwcm9wZXJ0eTogS1Byb3BlcnR5PCo+KTogVGVzdFByb3A8VD4ge1xuICAgICAgICBwcmludGxuKFwiTXkgc2F2aW5nIGtleSBpcyAke3RoaXMuamF2YUNsYXNzLnNpbXBsZU5hbWV9LiR7cHJvcGVydHkubmFtZX1cIilcbiAgICAgICAgcmV0dXJuIHRoaXNcbiAgICB9XG59XG5mdW4gbWFpbigpIHtcbiAgICBwcmludGxuKFRlc3QudGVzdClcbn0ifQ==) an example based on yours that should work:

```auto
import kotlin.reflect.KProperty

object Test {
    val test: Int by no_prop { 10 } // I want this
    val testTwo: Int by prop(::testTwo) { 10 } // I don't want this if possible
}

inline fun <reified T : Any> Test.no_prop(initializer: () -> T) = TestProp<T>()

inline fun <reified T : Any> Test.prop(prop: KProperty<*>, initializer: () -> T): TestProp<T> {
    return TestProp<T>()
}

class TestProp<T : Any> {
    operator fun getValue(thisRef: Any?, property: KProperty<*>) = 10 as T
    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {}
    operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): TestProp<T> {
        println("My saving key is ${this.javaClass.simpleName}.${property.name}")
        return this
    }
}
fun main() {
    println(Test.test)
}

```

It prints:

> My saving key is TestProp.test  
> My saving key is TestProp.testTwo  
> 10  
> Which is the desired effect.

---

<div class="post-metadata">

**Author:** ![Jonatino](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jonatino/32/8065_2.png) [@Jonatino](https://discuss.kotlinlang.org/u/Jonatino)\
**Post date:** [October 22, 2020, 8:07pm UTC](https://discuss.kotlinlang.org/t/possible-feature-kotlin-access-property-name-from-delegate/19597/3 "2020-10-22T20:07:39Z")

</div>

That is exactly what I was looking for. Thank you!!!
