gvlasov
gvlasov

Reputation: 20015

Why won't code that initializes a variable interface property in init block compile?

interface A {
    var a: Int
}

class AJunior : A {
    override var a: Int

    init {
        a = 3
    }
}

It won't compile because

Property must be initialized or be abstract

But it is initialized. I know I can write:

override var a: Int = 3

But why won't the first example compile? My guess would be that it is a bug or an intentional limitation to simplify compiler implementation, but I'm not sure.

Upvotes: 4

Views: 312

Answers (1)

gvlasov
gvlasov

Reputation: 20015

I reported this as a bug, but turns out this behavior is by design because:

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

Upvotes: 1

Related Questions