# How to check initial value of delegated property?

**URL:** <https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468>\
**Category:** Support\
**Created:** [October 12, 2020, 5:29pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468 "2020-10-12T17:29:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyhill](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andyhill](https://discuss.kotlinlang.org/u/andyhill)\
**Post date:** [October 12, 2020, 5:29pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/1 "2020-10-12T17:29:31Z")

</div>

Hi, I want a property of type Double to not have a negative value, including during class construction. I can do it this way:

```
import kotlin.properties.*
import kotlin.reflect.KProperty

class NonNeg(nonNeg: Double) {
    var nonNeg: Double by NonNegDelegate(nonNeg)
}

class NonNegDelegate(initVal: Double) {
	private var _nonNeg: Double = if (initVal >= 0.0) initVal else 0.0
	operator fun getValue(thisRef: Any?, property: KProperty<*>): Double {
		return _nonNeg
	}
	operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Double) {
		_nonNeg = if (value >= 0.0) value else 0.0
	}
}

fun main() {
    var myNonNeg = NonNeg(-2.0)
    println("${myNonNeg.nonNeg}") // 0.0
}

```

Is there a better way to do this with property delegation but without replicating the check in `setValue()` in the declaration of `_nonNeg`? Thanks.

---

<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 12, 2020, 5:54pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/2 "2020-10-12T17:54:45Z")

</div>

do the check in `getValue` instead with something like this:

```auto
operator fun getValue(thisRef: Any?, property: KProperty<*>): Double {
	return if (_nonNeg > 0.0) _nonNeg else 0.0
}

```

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [October 12, 2020, 6:12pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/3 "2020-10-12T18:12:43Z")

</div>

```kotlin
data class NonNegativeDouble(val value: Double) {
	init {
		require(value >= 0) { "Value must be greater or equal to 0." }
	}
	
	override fun toString() = value.toString()
}

data class NonNegative(val nonNegativeDouble: NonNegativeDouble)

```

If you want to protect your data constraints this way is probably better.

---

<div class="post-metadata">

**Author:** ![andyhill](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andyhill](https://discuss.kotlinlang.org/u/andyhill)\
**Post date:** [October 12, 2020, 6:19pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/4 "2020-10-12T18:19:42Z")

</div>

Thanks for the alternative! However, unless I kept the check in `setValue()`, which would then duplicate the check in `setValue()` and `getValue()`, the property would have negative value (even if inaccessible). (Further, if the value is read more often than written, this would run the check on every get . . . .)

Moving the goal post a bit: if the value is only allowed to be initialized to ≥ 0 and later set to ≥ 0 **or to retain its present value** :

```
class NonNegDelegate(initVal: Double) {
	private var _nonNeg: Double = if (initVal >= 0.0) initVal else 0.0
	operator fun getValue(thisRef: Any?, property: KProperty<*>): Double {
		return _nonNeg
	}
	operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Double) {
		_nonNeg = if (value >= 0.0) value else _nonNeg
	}
}

```

is there an alternative that uses property delegation but doesn’t replicate the check?

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [October 12, 2020, 6:28pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/5 "2020-10-12T18:28:41Z")

</div>

```kotlin
class NonNegDelegate(initVal: Double) {
	private var nonNeg: Double = initVal
		set(value) {
			require(value >= 0) { "Value must be greater or equal to 0." }
			field = value
		}
	
	init {
		require(nonNeg >= 0) { "Value must be greater or equal to 0." }
	}
	
	operator fun getValue(thisRef: Any?, property: KProperty<*>): Double = nonNeg
	
	operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Double) {
		nonNeg = value
	}
}

```

The only problem is as you probably noticed is that an initial field initialization doesn’t use setter, so the check needs to be done twice.

---

<div class="post-metadata">

**Author:** ![andyhill](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andyhill](https://discuss.kotlinlang.org/u/andyhill)\
**Post date:** [October 12, 2020, 6:36pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/6 "2020-10-12T18:36:52Z")

</div>

Thanks! It is as you said, “initial field initialization doesn’t use setter” summarizes it.

---

<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 12, 2020, 7:03pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/7 "2020-10-12T19:03:15Z")

</div>

> [@andyhill](#):
>
> ```auto
> class NonNegDelegate(initVal: Double) {
> private var _nonNeg: Double = if (initVal >= 0.0) initVal else 0.0
> operator fun getValue(thisRef: Any?, property: KProperty<*>): Double {
> return _nonNeg
> }
> operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Double) {
> _nonNeg = if (value >= 0.0) value else _nonNeg
> }
> }
> 
> ```

This should do it:

```auto
class NonNegDelegate private constructor(){
        constructor(initVal: Double): this() { setValue(null, null, initVal) }
	private var _nonNeg: Double = 0.0
	operator fun getValue(thisRef: Any?, property: KProperty<*>?): Double {
		return _nonNeg
	}
	operator fun setValue(thisRef: Any?, property: KProperty<*>?, value: Double) {
		_nonNeg = if (value >= 0.0) value else _nonNeg
	}
}

```

Or move your setter logic out into something else

---

<div class="post-metadata">

**Author:** ![andyhill](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andyhill](https://discuss.kotlinlang.org/u/andyhill)\
**Post date:** [October 12, 2020, 7:09pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/8 "2020-10-12T19:09:04Z")

</div>

That did do it! Yay! Thanks so much!

---

<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 12, 2020, 7:13pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/9 "2020-10-12T19:13:12Z")

</div>

No problem. You can also move that logic into the private property’s setter like this:

```auto
class NonNegDelegate private constructor(){
    constructor(initVal: Double): this() { _nonNeg = initVal }
	private var _nonNeg: Double = 0.0
        set(value) { field = if (value >= 0.0) value else field }
	operator fun getValue(thisRef: Any?, property: KProperty<*>?): Double = _nonNeg
	operator fun setValue(thisRef: Any?, property: KProperty<*>?, value: Double) = _nonNeg = value
}

```

---

<div class="post-metadata">

**Author:** ![andyhill](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andyhill](https://discuss.kotlinlang.org/u/andyhill)\
**Post date:** [October 12, 2020, 7:47pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/10 "2020-10-12T19:47:35Z")

</div>

Hm, I do like this one better. I guess because the logic is “closer” to the field? One less level of indirection? It’s good to know that one could call `setValue()` in the other solution though.

BTW, the last function assignment cannot be an assignment, according to the compiler:

```auto
	operator fun setValue(thisRef: Any?, property: KProperty<*>?, value: Double) = _nonNeg = value

```

needs to be:

```auto
	operator fun setValue(thisRef: Any?, property: KProperty<*>?, value: Double) { _nonNeg = value }

```

Thanks!

---

<div class="post-metadata">

**Author:** ![slavon.net](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@slavon.net](https://discuss.kotlinlang.org/u/slavon.net)\
**Post date:** [May 8, 2021, 12:22pm UTC](https://discuss.kotlinlang.org/t/how-to-check-initial-value-of-delegated-property/19468/11 "2021-05-08T12:22:06Z")

</div>

Delegate every time will be call to new queue. Object don’t save copy. If you want lifetime object you must use Lazy delegate like this

> <https://gist.github.com/slavonnet/4fca3c62f8d50d8dadd1687426415de0>
