# Compile time checked intialization in inline functions

**URL:** <https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663>\
**Category:** Language Design\
**Created:** [August 11, 2021, 10:31pm UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663 "2021-08-11T22:31:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![syaniv1234](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@syaniv1234](https://discuss.kotlinlang.org/u/syaniv1234)\
**Post date:** [August 11, 2021, 10:31pm UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/1 "2021-08-11T22:31:59Z")

</div>

Not sure if i’m using the right word, but by compile time checked initialization I mean this:

```auto
// this compiles
fun main() {
    var x: Int
    x = 10
    println(x)
}

```

I would like the language to support a case like this:

```auto
// this doesn't compile
fun main() {
    var x: Int
    unsuspendable {
        x = 10
    }
    println(x)
}
inline fun unsuspendable(block: () -> Unit) = block()

```

The purpose of unsuspendable is we have large blocks of code in a suspend function, and we basically want to mark off critical sections to make sure people get a compile time error if they try to add some suspend code there. There are work-arounds to this (like having unsuspendable return things), but I think it would be cool if this worked.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [August 11, 2021, 10:54pm UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/2 "2021-08-11T22:54:44Z")

</div>

Contracts allow this:

```auto
fun main() {
    var x: Int
    unsuspendable {
        x = 10
    }
    println(x)
}
inline fun unsuspendable(block: () -> Unit) {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
}

```

---

<div class="post-metadata">

**Author:** ![syaniv1234](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@syaniv1234](https://discuss.kotlinlang.org/u/syaniv1234)\
**Post date:** [August 12, 2021, 2:28am UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/3 "2021-08-12T02:28:42Z")

</div>

Thank you this is perfect!

---

<div class="post-metadata">

**Author:** ![syaniv1234](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@syaniv1234](https://discuss.kotlinlang.org/u/syaniv1234)\
**Post date:** [August 12, 2021, 2:35am UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/4 "2021-08-12T02:35:27Z")

</div>

For anyone else who sees this. I made a mistake making the function inline, because then it gets its suspendability from the outside scope. Here’s the final version of unsuspendable:

```auto
fun main() {
     runBlocking {
         var x: Int
         unsuspendable {
             // delay(100) // throws a compile error
             x = 10
         }
        println(x)
    }
}
fun unsuspendable(block: () -> Unit) {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
}

```

A variation is to add a generic return value.

---

<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:** [August 12, 2021, 3:56am UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/5 "2021-08-12T03:56:40Z")

</div>

In fact, you can still make it `inline`, but then make the parameter `crossinline`, which effectively prevents the suspendability being inherited from outside like this:

```auto
fun main() {
     runBlocking {
         var x: Int
         unsuspendable {
             // delay(100) // throws a compile error
             x = 10
         }
        println(x)
    }
}
inline fun unsuspendable(crossinline block: () -> Unit) {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
}
```

---

<div class="post-metadata">

**Author:** ![syaniv1234](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@syaniv1234](https://discuss.kotlinlang.org/u/syaniv1234)\
**Post date:** [August 12, 2021, 6:37am UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/6 "2021-08-12T06:37:10Z")

</div>

Hmm interesting. Ya the downside is it prevents nonlocal returns, which I don’t want to do.

---

<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:** [August 13, 2021, 5:32pm UTC](https://discuss.kotlinlang.org/t/compile-time-checked-intialization-in-inline-functions/22663/7 "2021-08-13T17:32:56Z")

</div>

Non-local returns, AFAIK, are also prevented if you use a normal (i.e. non-inline) fun with a lambda parameter.
