# Lateinit primitive types

**URL:** <https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406>\
**Category:** Language Design\
**Created:** [March 15, 2022, 3:36am UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406 "2022-03-15T03:36:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![donaldtam](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@donaldtam](https://discuss.kotlinlang.org/u/donaldtam)\
**Post date:** [March 15, 2022, 3:36am UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/1 "2022-03-15T03:36:06Z")

</div>

I am sure this has been asked before, why no lateinit primitive types?  
However, I am just confused with the answer that lateinit primitives are technically impossible

From what I know the null value is used as uninitialized markers for object types underlyingly.  
Why can’t we do the same with primitives using boxed primitives (Integer instead of int)?

We are able to do Int? but not lateinit var: Int  
why?

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [March 15, 2022, 10:26am UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/2 "2022-03-15T10:26:39Z")

</div>

I think the fundamental reason for this not to work is that `lateinit` doesn’t change the type.

With this suggestion - which would work - then adding `lateinit` would change the type of the underlying object.

---

<div class="post-metadata">

**Author:** ![donaldtam](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@donaldtam](https://discuss.kotlinlang.org/u/donaldtam)\
**Post date:** [March 15, 2022, 10:40am UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/3 "2022-03-15T10:40:17Z")

</div>

i mean adding the question mark to Int also changes its type from primitive to boxed primitive too, right?

---

<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:** [March 15, 2022, 1:15pm UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/4 "2022-03-15T13:15:59Z")

</div>

Yes, but that’s a direct change to the type, while lateinit is “further away” geographically from the type. I agree that it should just be allowed, but I can see why it isn’t a thing yet.  
There is a bug for it on Youtrack: [https://youtrack.jetbrains.com/issue/KT-15284](https://youtrack.jetbrains.com/issue/KT-15284)

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [March 15, 2022, 7:26pm UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/5 "2022-03-15T19:26:59Z")

</div>

Anyway, for now you can use `Delegates.notNull()` for a very similar effect:

```kotlin
var num by Delegates.notNull<Int>()

// println(num) // IllegalStateException
num = 8
println(num) // 8

```

---

<div class="post-metadata">

**Author:** ![unrealprox](https://avatars.discourse-cdn.com/v4/letter/u/b487fb/32.png) [@unrealprox](https://discuss.kotlinlang.org/u/unrealprox)\
**Post date:** [September 2, 2024, 3:38pm UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/6 "2024-09-02T15:38:33Z")

</div>

Currently (version 1.9.24), instead of lateinit primitive property I have to declare initialized property:

```auto
class Example {
    lateinit var property1: String

    var property2: Boolean = false

    lateinit var property3: StringBuilder

    var property4: Int = 0
}

```

- this looks quite ugly near lateinit object properties. Also, I have to declare some random value.

Why don’t you allow lateinit primitive property with similar initialization for backed field, ignoring scenario where property is not initialized to throw exception

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [September 2, 2024, 6:49pm UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/7 "2024-09-02T18:49:15Z")

</div>

> [@unrealprox](#):
>
> ignoring scenario where property is not initialized to throw exception

So if there’s no exception, what do you get when you try to read it? Some random value?

---

<div class="post-metadata">

**Author:** ![unrealprox](https://avatars.discourse-cdn.com/v4/letter/u/b487fb/32.png) [@unrealprox](https://discuss.kotlinlang.org/u/unrealprox)\
**Post date:** [September 3, 2024, 6:11am UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/8 "2024-09-03T06:11:45Z")

</div>

Yes, some random value.

Because:

1. In good case scenario (if I set value later) it doesn’t matter
2. In bad case scenario the behaviour is the same as in my current code:

```auto
var property2: Boolean = false

```

- where I get “random” value. I wouldn’t be able to determine if property was initialized or not

---

<div class="post-metadata">

**Author:** ![unrealprox](https://avatars.discourse-cdn.com/v4/letter/u/b487fb/32.png) [@unrealprox](https://discuss.kotlinlang.org/u/unrealprox)\
**Post date:** [June 11, 2025, 6:31am UTC](https://discuss.kotlinlang.org/t/lateinit-primitive-types/24406/9 "2025-06-11T06:31:53Z")

</div>

Coding for some time I am convinced that it is better not only allow lateinit primitives, but also lateinit nullable types. I don’t think that current restrictions for some exception are justified.

Currently, I am cope with such ugly code:

```auto
    class Example {
       lateinit var property1: String

       var property2: String? = STUB_NULLABLE // val STUB_NULLABLE: Nothing? = null

       var property3: Int = STUB_INT // const val STUB_INT: Int = 0
    }

```
