# Delegate property class constructors

**URL:** <https://discuss.kotlinlang.org/t/delegate-property-class-constructors/18847>\
**Category:** Language Design\
**Created:** [August 20, 2020, 2:11am UTC](https://discuss.kotlinlang.org/t/delegate-property-class-constructors/18847 "2020-08-20T02:11:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![innov8ian](https://avatars.discourse-cdn.com/v4/letter/i/ee7513/32.png) [@innov8ian](https://discuss.kotlinlang.org/u/innov8ian)\
**Post date:** [August 20, 2020, 2:11am UTC](https://discuss.kotlinlang.org/t/delegate-property-class-constructors/18847/1 "2020-08-20T02:11:24Z")

</div>

There is one design decision with delegate properties I never understood.

The answer to this question by @kyay10 provided the solution, so now this post is being becomes a post for anyone else who has the same questions, rather than remaining as a question.

Why was this format (From the documentation):

```
class Example {
    var p: String by Delegate()
}
import kotlin.reflect.KProperty

class Delegate {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "$thisRef, thank you for delegating '${property.name}' to me!"
    }
 
    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("$value has been assigned to '${property.name}' in $thisRef.")
    }
}

```

chosen and not this format?

```
import kotlin.reflect.KProperty

class Delegate(val thisRef: Any?, val property: KProperty<*>) {
    operator fun getValue(): String {
        return "$thisRef, thank you for delegating '${property.name}' to me!"
    }
 
    operator fun setValue(value: String) {
        println("$value has been assigned to '${property.name}' in $thisRef.")
    }
}

```

Using this approach from other languages can be difficult to convert, as a table of delegate objects is required. If the table of delegate objects should contain the property names, and this becomes messy with the above syntax. That syntax does allow for the same instance of the Delegate class to be reused for multiple properties, but does not provide the delegate object itself with access to the parent object or the KProperty object.

I made a language design request to allow an alternate syntax supporting the Delegate constructor receiving the `thisRef` and `property` parameters.

In fact, as answered below, this already exists by way of the `provideDelegate` function.

---

<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:** [August 20, 2020, 7:43am UTC](https://discuss.kotlinlang.org/t/delegate-property-class-constructors/18847/2 "2020-08-20T07:43:22Z")

</div>

For your use case, you can define a `provideDelegate` operator so that you’ll have access to the `thisRef` and `property` at creation time. Assuming that your code is currently like this:

```auto
class Delegate(val initialValue: String) {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "$thisRef, thank you for delegating '${property.name}' to me! Your initial value was $initialValue."
    }
 
    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("$value has been assigned to '${property.name}' in $thisRef. Your initial value was $initialValue.")
    }
}

```

Then you can do this:

```auto
class Delegate(val initialValue: String, val thisRef: Any?, val property: KProperty<*>) {
    operator fun getValue(ignored: Any?, ignored2: KProperty<*>): String {
        return "$thisRef, thank you for delegating '${property.name}' to me! Your initial value was $initialValue."
    }
 
    operator fun setValue(ignored: Any?, ignored2: KProperty<*>, value: String) {
        println("$value has been assigned to '${property.name}' in $thisRef. Your initial value was $initialValue.")
    }
}
class DelegateProvider(val initialValue) {
    operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): Delegate {
        return Delegate(initialValue, thisRef, property)
    }
}
// The use site will look like this:
class Example {
    var p: String by DelegateProvider("hello")
}

```

---

<div class="post-metadata">

**Author:** ![innov8ian](https://avatars.discourse-cdn.com/v4/letter/i/ee7513/32.png) [@innov8ian](https://discuss.kotlinlang.org/u/innov8ian)\
**Post date:** [August 20, 2020, 8:04am UTC](https://discuss.kotlinlang.org/t/delegate-property-class-constructors/18847/3 "2020-08-20T08:04:27Z")

</div>

Thank you… i had not used provideDelegate() and had missed that it was there!

I need to do some more work with my actual use case, but that certainly seems to pass the first hurdle.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [August 21, 2020, 11:21am UTC](https://discuss.kotlinlang.org/t/delegate-property-class-constructors/18847/4 "2020-08-21T11:21:04Z")

</div>

Please note that you can make `getValue` and `setValue` inline. In that case the new optimizer should even not generate the properties in use.
