# Initializing vals in closure?

**URL:** <https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422>\
**Category:** Uncategorized\
**Created:** [January 19, 2016, 8:24pm UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422 "2016-01-19T20:24:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [January 19, 2016, 8:24pm UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422/1 "2016-01-19T20:24:15Z")

</div>

Hi,

I asked about this a while ago ([old post](https://discuss.kotlinlang.org/t/initializing-val-properties-in-closure/511)). Curious if anything has changed, as I’m cleaning up some old code.

Basically, can I tell it that `x` is safely initialized?

```
fun help(f: () -> Unit) { f() }

class Foo {
    val x: Int
    init {
        help {
            x = 10
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [January 20, 2016, 10:25am UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422/2 "2016-01-20T10:25:50Z")

</div>

I don’t think so.  
Within class “Foo”, you have no clue what “help” function is going to do with your lambda. Maybe help function never executes the given lambda (f). So the compiler complains about “x” not being initialized.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [January 20, 2016, 11:03am UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422/3 "2016-01-20T11:03:39Z")

</div>

Not allowed under any circumstances at the moment.

This may be supported some time later for the case when `help` is inlined. That is, if we get around to “seeing through” inilne function bodies"

---

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [January 20, 2016, 1:57pm UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422/4 "2016-01-20T13:57:37Z")

</div>

I could file a feature request, if you think it’s possible and worth considering an attribute that the compiler checks, like:

```
fun help(@mustcall f: () -> Unit) { ... }

```

Meaning: if `help` doesn’t throw, `f` was called.

The [builder pattern](https://kotlinlang.org/docs/reference/type-safe-builders.html) is such a nice feature for building UI component classes, but without this my constructors can be a bit awkward.

Rob

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [January 21, 2016, 10:24pm UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422/5 "2016-01-21T22:24:03Z")

</div>

For a signature like that, could you maybe change it to pass through the return value?

x = help { 10 }

for instance.

---

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [January 21, 2016, 10:44pm UTC](https://discuss.kotlinlang.org/t/initializing-vals-in-closure/1422/6 "2016-01-21T22:44:28Z")

</div>

Unfortunately, in the real code the body is creating UI structures like HTML DOM elements, and setting a number of instance variables (not just one, like my example) to pieces of the DOM.

Rob
