Why is there no code that initializes the variable's interface property in the compilation of the init block?

interface A {
    var a: Int
}

class AJunior : A {
    override var a: Int

    init {
        a = 3
    }
}

It will not compile because

Property must be initialized or abstract.

But it is initialized. I know I can write:

override var a: Int = 3

But why doesn't the first example compile? I assume this is a bug or an intentional limitation to simplify the implementation of the compiler, but I'm not sure.

+4
source share
1 answer

I reported this as an error , but finds out that this is a design behavior because:

you may have code in the init block that could observe the property in its uninitialized state

+1
source

Source: https://habr.com/ru/post/1618202/


All Articles