Scala initialization behaviour

Viewed 3439

Please look at the following code.

trait MyTrait { val myVal : String }

class MyClass extends MyTrait { val myVal = "Value" }

class MyClass2(val myVal: String) extends MyTrait 

Why does the initialization order differ in case of MyClass and MyClass2? The constructor of MyClass will be as

MyClass() {
  MyTrait$class.$init$(this);
  myVal = value
}

The constructor of MyClass2 will be

MyClass2(String myVal) { this.myVal = myVal; MyTrait$class.$init$(this) }

I think the initialization order should be as MyClass2's constructor does, the same for both cases.

1 Answers
Related