# Access members of custom delegates

**URL:** <https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648>\
**Category:** Language Design\
**Created:** [May 2, 2018, 5:39am UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648 "2018-05-02T05:39:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eb2501](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/eb2501/32/5075_2.png) [@eb2501](https://discuss.kotlinlang.org/u/eb2501)\
**Post date:** [May 2, 2018, 5:39am UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648/1 "2018-05-02T05:39:56Z")

</div>

Hi,

I would like to access a function defined on my delegate:

```
class Delegate : ReadOnlyProperty<Any, Int> {

    override fun getValue(thisRef: Any, property: KProperty<*>): Int {
        return 12
    }

    fun f() {}
}

class Provider {
    operator fun provideDelegate(thisRef: Any, prop: KProperty<*>): Delegate {
        prop.isAccessible = true
        return Delegate()
    }
}

class MyClass {
    val x by Provider()
}

fun main() {
    val c = MyClass()
    val d = c::x.getDelegate()
}

```

The problem is that `getDelegate()` throws this:

```
kotlin.reflect.full.IllegalPropertyDelegateAccessException: Cannot obtain the delegate of a non-accessible property. Use "isAccessible = true" to make the property accessible

```

Not sure what else I can do to make the delegate “accessible”.

On a broader topic, I would love to be able to call `c::x.f()` as simply as this. Any way this could be done (or any plans to make it happen)?

Thank you.

---

<div class="post-metadata">

**Author:** ![noo.blaster](https://avatars.discourse-cdn.com/v4/letter/n/ac91a4/32.png) [@noo.blaster](https://discuss.kotlinlang.org/u/noo.blaster)\
**Post date:** [May 2, 2018, 1:21pm UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648/3 "2018-05-02T13:21:54Z")

</div>

after looking more at your code, I think you don’t understand why your `prop.isAccessible = true` is not working. I think the isAccessible = true is applied only before setting a delegate, so that delegate has not been set to accessible…

What you need is to setAccessible after setting delegate… which I not supported in kotlin (delegateProvider only allow to handle delegate creation, not “wiring”)

I still believe you maybe are doing something wrong & there should be another way.

Maybe an extension could help you

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 2, 2018, 1:45pm UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648/4 "2018-05-02T13:45:58Z")

</div>

I don’t really now why your solution is not working but take a look at this workaround:

> <https://stackoverflow.com/questions/44844933/more-fun-with-kotlin-delegates/44845431#44845431>

---

<div class="post-metadata">

**Author:** ![eb2501](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/eb2501/32/5075_2.png) [@eb2501](https://discuss.kotlinlang.org/u/eb2501)\
**Post date:** [May 2, 2018, 9:44pm UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648/5 "2018-05-02T21:44:37Z")

</div>

Thanks, your answer on stackoverflow worked for my case.

I could have spent hours on this without this!

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 2, 2018, 10:08pm UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648/6 "2018-05-02T22:08:58Z")

</div>

Not my solution. Thats just the first thing I found on google 😜

---

<div class="post-metadata">

**Author:** ![eb2501](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/eb2501/32/5075_2.png) [@eb2501](https://discuss.kotlinlang.org/u/eb2501)\
**Post date:** [May 3, 2018, 7:30am UTC](https://discuss.kotlinlang.org/t/access-members-of-custom-delegates/7648/7 "2018-05-03T07:30:06Z")

</div>

Damnit 🙂
