# Treat init as a regular function block

**URL:** <https://discuss.kotlinlang.org/t/treat-init-as-a-regular-function-block/17762>\
**Category:** Language Design\
**Created:** [May 19, 2020, 5:38pm UTC](https://discuss.kotlinlang.org/t/treat-init-as-a-regular-function-block/17762 "2020-05-19T17:38:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterhenryd](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@peterhenryd](https://discuss.kotlinlang.org/u/peterhenryd)\
**Post date:** [May 19, 2020, 5:38pm UTC](https://discuss.kotlinlang.org/t/treat-init-as-a-regular-function-block/17762/1 "2020-05-19T17:38:30Z")

</div>

Kotlin should be able to treat init as a regular function block which returns Unit.

This would be helpful for doing single call stuff like  
init = println(“Hey there!”)

To aid with consistency, Kotlin could treat init as an actual function which is overridden from perhaps, Any. This is just an extra to this request because I’m not sure how it would pan out.

---

<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 19, 2020, 7:22pm UTC](https://discuss.kotlinlang.org/t/treat-init-as-a-regular-function-block/17762/2 "2020-05-19T19:22:31Z")

</div>

> [@peterhenryd](#):
>
> This would be helpful for doing single call stuff like  
> init = println(“Hey there!”)

Maybe. I don’t really dislike this idea, but I don’t see a real usecase for this. Maybe for logging a debug statement? Not sure.

> [@peterhenryd](#):
>
> To aid with consistency, Kotlin could treat init as an actual function which is overridden from perhaps, Any. This is just an extra to this request because I’m not sure how it would pan out.

There are a few problems with this. The first is initialization of `val` properties.

```kotlin
class Foo(a: Int) {
    val a: Int
    init {
        this.a = a
    }
}

```

In this case `init` can’t be called as a regular function, because it would set `a` for a second time.

The second problem is the function parameters. Are they the same as for the primary constructor?

```kotlin
class Foo(val a: Int) {
    init = println("init Foo")
}
val f = Foo()
f.init()
// or
f.init(5)

```

You could say it depends on the parameters that are actually used inside the init block, but that would lead to situations where just logging a parameter would change the ABI of a class (which might be problematic). Also it would make implementing this much harder.

A last problem is the number of function this creates. While function count isn’t really a problem on PC it is a problem for android. Adding a function for each class is a significant amount and should be done only if there is a good reason.

TL;DR: Maybe for the new init syntax, probably not for the ability to call the `init` block as a function.
