# StackOverflow with Custom Delegate

**URL:** <https://discuss.kotlinlang.org/t/stackoverflow-with-custom-delegate/10190>\
**Category:** Language Design\
**Created:** [November 6, 2018, 11:55am UTC](https://discuss.kotlinlang.org/t/stackoverflow-with-custom-delegate/10190 "2018-11-06T11:55:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rikyzan](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@rikyzan](https://discuss.kotlinlang.org/u/rikyzan)\
**Post date:** [November 6, 2018, 11:55am UTC](https://discuss.kotlinlang.org/t/stackoverflow-with-custom-delegate/10190/1 "2018-11-06T11:55:43Z")

</div>

Hi, i’ve some trouble with custom delegates.

i simply want to create a custom delegate that checks if the value passed is greater than 0, and updates the value if the condition is respected.

how can i do it? thanks

class MyDelegate() : ReadWriteProperty\<PositiveNumber, Int\>  
{  
 override fun getValue(thisRef: PositiveNumber, property: KProperty\<\*\>): Int {  
 return thisRef.number  
 }

override fun setValue(thisRef: PositiveNumber, property: KProperty\<\*\>, value: Int) {  
 if(value \> 0) thisRef.number=value  
 }  
}

class PositiveNumber()  
{  
 var number : Int by MyDelegate()  
}

---

<div class="post-metadata">

**Author:** ![Varia](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Varia](https://discuss.kotlinlang.org/u/Varia)\
**Post date:** [November 6, 2018, 7:54pm UTC](https://discuss.kotlinlang.org/t/stackoverflow-with-custom-delegate/10190/2 "2018-11-06T19:54:01Z")

</div>

The important thing to note when using a delegated property is that the class using the delegate (in your case `PositiveNumber` does not have a backing field for the property. Normally, it is the delegates responsibility to hold the value. The class using the delegate just delegates all reads and writes on that property to the delegate. In your case, that means that whenever the `number` property of a `PositiveNumber` is accessed, it calls one of the methods on the delegate. The delegate in turn tries to access the `number` property of the `PositiveNumber`. This results in infinite recursion.

For the case you present, I would not really consider a delegated property to be the right solution. In your case, I would just implement `PositiveNumber.number` as a normal non-delegated property with a custom setter. Alternatively it could be an inline class.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [November 6, 2018, 7:56pm UTC](https://discuss.kotlinlang.org/t/stackoverflow-with-custom-delegate/10190/3 "2018-11-06T19:56:54Z")

</div>

Are you considered built-in `observable` or `vetoable`?

> **[Delegates - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.properties/-delegates/)**

---

<div class="post-metadata">

**Author:** ![rikyzan](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@rikyzan](https://discuss.kotlinlang.org/u/rikyzan)\
**Post date:** [November 13, 2018, 10:58am UTC](https://discuss.kotlinlang.org/t/stackoverflow-with-custom-delegate/10190/4 "2018-11-13T10:58:26Z")

</div>

thanks for your answers! they were in my spam emails, so i’ve just noticed 😅 😓

in conclusion, a custom delegate can’t work as getter/setter for a property, because it would generate an infinite recursion.

thanks to all!
