Hello, is this code supposed to throw an `InvalidM...
# kotlin-native
t
Hello, is this code supposed to throw an
InvalidMutabilityException
? If you move the
init
to the end (after the lazy val) it does not throw the exception.
Copy code
fun main() {
    Example()
}

class Example {

    init {
        freeze()
    }

    val value by lazy { 1 }
}
d
Ideally it shouldn't but it does. It's bug territory.
😁 1
The same problem exists for subclasses. If the parent class freezes in
init
, then the subclass can't initialize itself.
o
it's kind of intended. After freeze any mutation (including init of the field) is disallowed. Following code throws the same exception as well:
Copy code
class Example {

    init {
        freeze()
    }

    val value = 1
}
t
@olonho Ok, thanks that’s clear to me. So can I assume that freezing in a class happens from “top to bottom”? Is it safe to freeze a class inside init if it contains field above the init?
o
yes, exactly, init order is from top to bottom
👍 1