# ReenteringLazyValueComputationException with recursive-ish code

**URL:** <https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386>\
**Category:** Support\
**Created:** [December 11, 2019, 5:31am UTC](https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386 "2019-12-11T05:31:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crummy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/crummy/32/7034_2.png) [@crummy](https://discuss.kotlinlang.org/u/crummy)\
**Post date:** [December 11, 2019, 5:31am UTC](https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386/1 "2019-12-11T05:31:26Z")

</div>

Here’s a simple class where I modify the behaviour of the `output` function each time it is called:

```auto
class PaintingRobot {
    val turn = { direction: Int ->
        output = this.paint
    }

    val paint = { colour: Int ->
        output = this.turn
    }

    var output: (Int) -> Unit = turn
}

```

IntelliJ 2019.3 says: “Type checking has run into a recursive problem.”  
Running build on the project reports: “Error:Kotlin: [Internal Error] org.jetbrains.kotlin.util.ReenteringLazyValueComputationException”  
I tried it in [play.kotlinlang.org](http://play.kotlinlang.org) and just get a “Service temporarily available” error: [Kotlin Playground: Edit, Run, Share Kotlin Code Online](https://pl.kotl.in/D5jVzCySv)

I think this should work. Right?

---

<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:** [December 11, 2019, 7:39am UTC](https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386/2 "2019-12-11T07:39:33Z")

</div>

You should specify explicitly the type of `turn` and `paint`.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [December 11, 2019, 10:22am UTC](https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386/3 "2019-12-11T10:22:44Z")

</div>

`turn` and `paint` are declared and initialized before `output`, which means that the 2 lambdas have not been typed at all.  
Explicit the type on the first two and let the `output` type be inferred.

---

<div class="post-metadata">

**Author:** ![crummy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/crummy/32/7034_2.png) [@crummy](https://discuss.kotlinlang.org/u/crummy)\
**Post date:** [December 11, 2019, 10:45am UTC](https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386/4 "2019-12-11T10:45:04Z")

</div>

Thanks, that did it. This works:

```auto
class PaintingRobot {
    val turn: (Int) -> Unit = { _: Int ->
        output = this.paint
    }

    val paint: (Int) -> Unit = { _: Int ->
        output = this.turn
    }

    var output: (Int) -> Unit = turn
}

```

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [December 11, 2019, 11:51pm UTC](https://discuss.kotlinlang.org/t/reenteringlazyvaluecomputationexception-with-recursive-ish-code/15386/5 "2019-12-11T23:51:51Z")

</div>

To declare a lambda, you need to explicit its type. Why is needed is easily understandable if you just think about an extension lambda. The only way to tell the compile that the lambda you are creating has a particular receiver is to declare it!

Also you can just leave `var output = turn`
