# Postponing defining val to init now gives warning (and in the future error). This seem to hamper clean code

**URL:** <https://discuss.kotlinlang.org/t/postponing-defining-val-to-init-now-gives-warning-and-in-the-future-error-this-seem-to-hamper-clean-code/27536>\
**Category:** Uncategorized\
**Created:** [October 25, 2023, 11:05am UTC](https://discuss.kotlinlang.org/t/postponing-defining-val-to-init-now-gives-warning-and-in-the-future-error-this-seem-to-hamper-clean-code/27536 "2023-10-25T11:05:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TorRanfelt](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TorRanfelt](https://discuss.kotlinlang.org/u/TorRanfelt)\
**Post date:** [October 25, 2023, 11:05am UTC](https://discuss.kotlinlang.org/t/postponing-defining-val-to-init-now-gives-warning-and-in-the-future-error-this-seem-to-hamper-clean-code/27536/1 "2023-10-25T11:05:02Z")

</div>

The following code now gives the warning “Property must be initialized, be final, or be abstract. This warning will become an error in future releases”.

I really don’t think the proposed solutions give better code. How does the Kotlin developers envision this ideally should be done when I need to run initialize() before I can set id and textEntry?

```auto
class FooBar(
    idElements: List<Int>,
    defaultTextEntry: String? = null
) : IFooBar {
    override val id: DotIntID
    override val textEntry: DpTextEntry?

    init {
        val (id, textEntry) = initialize(idElements, defaultTextEntry)
        this.id = id
        this.textEntry = textEntry
    }

```

---

<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:** [October 25, 2023, 12:18pm UTC](https://discuss.kotlinlang.org/t/postponing-defining-val-to-init-now-gives-warning-and-in-the-future-error-this-seem-to-hamper-clean-code/27536/2 "2023-10-25T12:18:54Z")

</div>

I can’t seem to recreate the error? Here’s what I tried ([playground](https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS45LjEwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsImpzQ29kZSI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiaW50ZXJmYWNlIElGb29CYXIge1xuICAgIHZhbCBpZDogRG90SW50SURcbiAgICB2YWwgdGV4dEVudHJ5OiBEcFRleHRFbnRyeT9cbn1cblxuY2xhc3MgRG90SW50SURcbmNsYXNzIERwVGV4dEVudHJ5XG5cbmNsYXNzIEZvb0JhcihcbiAgICBpZEVsZW1lbnRzOiBMaXN0PEludD4sXG4gICAgZGVmYXVsdFRleHRFbnRyeTogU3RyaW5nPyA9IG51bGxcbikgOiBJRm9vQmFyIHtcbiAgICBvdmVycmlkZSB2YWwgaWQ6IERvdEludElEXG4gICAgb3ZlcnJpZGUgdmFsIHRleHRFbnRyeTogRHBUZXh0RW50cnk/XG5cbiAgICBpbml0IHtcbiAgICAgICAgdmFsIChpZCwgdGV4dEVudHJ5KSA9IGluaXRpYWxpemUoaWRFbGVtZW50cywgZGVmYXVsdFRleHRFbnRyeSlcbiAgICAgICAgdGhpcy5pZCA9IGlkXG4gICAgICAgIHRoaXMudGV4dEVudHJ5ID0gdGV4dEVudHJ5XG4gICAgfVxufVxuXG5cbmZ1biBGb29CYXIuaW5pdGlhbGl6ZShpZEVsZW1lbnRzOiBMaXN0PEludD4sIGRlZmF1bHRUZXh0RW50cnk6IFN0cmluZz8pOiBQYWlyPERvdEludElELCBEcFRleHRFbnRyeT8+ID0gVE9ETygpIn0=)):

```auto
interface IFooBar {
    val id: DotIntID
    val textEntry: DpTextEntry?
}

class DotIntID
class DpTextEntry

class FooBar(
    idElements: List<Int>,
    defaultTextEntry: String? = null
) : IFooBar {
    override val id: DotIntID
    override val textEntry: DpTextEntry?

    init {
        val (id, textEntry) = initialize(idElements, defaultTextEntry)
        this.id = id
        this.textEntry = textEntry
    }
}

fun FooBar.initialize(idElements: List<Int>, defaultTextEntry: String?): Pair<DotIntID, DpTextEntry?> = TODO()

```

---

<div class="post-metadata">

**Author:** ![TorRanfelt](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@TorRanfelt](https://discuss.kotlinlang.org/u/TorRanfelt)\
**Post date:** [October 30, 2023, 9:14am UTC](https://discuss.kotlinlang.org/t/postponing-defining-val-to-init-now-gives-warning-and-in-the-future-error-this-seem-to-hamper-clean-code/27536/3 "2023-10-30T09:14:00Z")

</div>

With IntelliJ IDEA 2023.2.3 where I put it into a huge code base:

 ![must_be_initialized](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/f/f0fdeeb2933a2bcfb2a506f89629dbe1eeef2029.png)

---

<div class="post-metadata">

**Author:** ![shruggie](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@shruggie](https://discuss.kotlinlang.org/u/shruggie)\
**Post date:** [November 13, 2023, 7:30am UTC](https://discuss.kotlinlang.org/t/postponing-defining-val-to-init-now-gives-warning-and-in-the-future-error-this-seem-to-hamper-clean-code/27536/4 "2023-11-13T07:30:08Z")

</div>

We have a very similar case.

Is this the same as [https://youtrack.jetbrains.com/issue/KT-61228/False-positive-MUSTBEINITIALIZEDORFINALORABSTRACTWARNING-for-effectively-final-properties](https://youtrack.jetbrains.com/issue/KT-61228/False-positive-MUSTBEINITIALIZEDORFINALORABSTRACTWARNING-for-effectively-final-properties) ?  
If so, it looks like it’ll be fixed in 1.9.30.
