4
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.

1
  • 1
    I see no reason for that not to work. Think it's a bug Commented Dec 2, 2015 at 12:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm wondering about the same question you asked in that ticket, why would the val case work. If you try to reference 'a' before it's initialized in the init block you get the error 'Variable 'a' must be initialized.' I don't see any reason it wouldn't be the same for a var.
@eski You might want to join the discussion at the bug tracker.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.