# Lateinit var for inline methods

**URL:** <https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487>\
**Category:** Language Design\
**Created:** [April 19, 2018, 5:12pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487 "2018-04-19T17:12:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [April 19, 2018, 5:12pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/1 "2018-04-19T17:12:12Z")

</div>

If I have an inline method, and I make a constructor assignment within a lambda passed to that inline method. Why does the variable have to be lateinit? If the lamba isn’t marked as noinline or crossinline, isn’t that safe to be private val bar: Bar instead of private lateinit var bar: Bar?

```
class A {
    private lateinit var bar: Bar // Why does this need to be lateinit?
    init {
        foo {
            bar = Bar()
        }
    }
}
inline fun foo(init: ()->Unit) {
    init()
}

```

---

<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:** [April 19, 2018, 5:35pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/2 "2018-04-19T17:35:29Z")

</div>

In your example this would be save, but there is no reasonable way for the compiler to know that. There is no guarantee that `foo` will call `init`. I mean yeah sure, you could do some sort of static analysis but this is a problem which if I remember correctly is proven to be not solvable on a turing machine.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [April 19, 2018, 6:46pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/3 "2018-04-19T18:46:02Z")

</div>

I thought inline functions had control flow analysis.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [April 19, 2018, 7:58pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/4 "2018-04-19T19:58:09Z")

</div>

Currently inline functions are implemented through the bytecode of the function being mangled for inline use. The ast is lost at that point.

---

<div class="post-metadata">

**Author:** ![roryaburks](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@roryaburks](https://discuss.kotlinlang.org/u/roryaburks)\
**Post date:** [April 20, 2018, 8:41pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/5 "2018-04-20T20:41:18Z")

</div>

> [@Wasabi375](#):
>
> I mean yeah sure, you could do some sort of static analysis but this is a problem which if I remember correctly is proven to be not solvable on a turing machine.

Really? That doesn’t seem right. Surely determining whether a codepoint is hit exactly once in all non-Nothing cases isn’t an unsolvable problem in the general. Though I guess once you start recursively passing it around it might be. Well you could always have a keyword “once” for lambdas and have it so that a function that takes a “once” lambda either consumes the lambda for all cases or passes it to another “once” function that consumes it. At that point you’re pretty much just mimicking val behavior when it comes to verifying at or before compile time.

---

<div class="post-metadata">

**Author:** ![dico.karssiens](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dico.karssiens](https://discuss.kotlinlang.org/u/dico.karssiens)\
**Post date:** [April 28, 2018, 5:26am UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/6 "2018-04-28T05:26:16Z")

</div>

There’s actually an interesting construct found in the stdlib source code, where inline functions with functions as parameters define a contract. Something like:

```auto
contract {
  callsInPlace = true
}

```

or something like that. The `contract` method is an inlined function with no code in it, so it is not actually changing anything at runtime, though the compiler will be able to analyze it at compile time.  
There are a few more variables that can be set, I don’t know exactly what they are called or what the function is, but one application is that you can use this to tell the compiler that the function parameter is used one time synchronously, every time.

Unfortunately, this construct is internal.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [May 9, 2018, 1:04pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/7 "2018-05-09T13:04:07Z")

</div>

JB: Any chance there are plans to make that contract metadata public?

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [May 9, 2018, 1:12pm UTC](https://discuss.kotlinlang.org/t/lateinit-var-for-inline-methods/7487/8 "2018-05-09T13:12:38Z")

</div>

Although right now the contract stuff doesn’t actually do what I was hoping:

```
private val t: String

init {
	3.apply {
		t = "" // Error, captured member values initialization is forbidden due to possible reassignment
	}
}

```

This does look descriptive of what I want though!

```
contract {
	callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}

```
