# Kinda lazy, kinda lateinit, resettable property

**URL:** <https://discuss.kotlinlang.org/t/kinda-lazy-kinda-lateinit-resettable-property/17782>\
**Category:** Support\
**Created:** [May 21, 2020, 9:02pm UTC](https://discuss.kotlinlang.org/t/kinda-lazy-kinda-lateinit-resettable-property/17782 "2020-05-21T21:02:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [May 21, 2020, 9:02pm UTC](https://discuss.kotlinlang.org/t/kinda-lazy-kinda-lateinit-resettable-property/17782/1 "2020-05-21T21:02:40Z")

</div>

I’ve got an object that has some expensive-to-compute values which rely on each other. So I considered making them `by lazy`. And I can make sure that only one thread is calculating it with the default `LazyThreadSafetyMode.SYNCHRONIZED`

But in the chain of **myClass.c depends on myClass.b depends on myClass.a** , I don’t think I can say “b changed, which invalidated c”. I think I need a way to say “clear out the lazy value and recompute next time it is requested”

I hacked something together. It isn’t pretty, or synchronized, and all users of this solution need to !! to avoid nulls that will never happen. Is there a better way to do this? A custom byLazyButResettable perhaps?

```auto
private var b: MyThing? = null
  get() = field ?: a!!.doStuff().also { field = it }
  set(value) {
    c = null
    field = value
  }

```

---

<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 21, 2020, 10:41pm UTC](https://discuss.kotlinlang.org/t/kinda-lazy-kinda-lateinit-resettable-property/17782/2 "2020-05-21T22:41:00Z")

</div>

> [@benjaminhill](#):
>
> A custom byLazyButResettable perhaps?

Not that I am aware of. But you should be able to build one yourself quite easily. Just take a look at the sources for `lazy`. If I remember correctly the use something like

```kotlin
object EmptyValue
private var _value: Any? = EmptyValue
val value: T get() = if(_value == EmptyValue) { _value = _value as T} else _value as T

```

plus some syncronization. The `EmptyValue` object is used instead of `null` to allow for nullable types.
