How do I pre-initialize a val and extend a class in Scala?
e.g.:
object Start {
def main(args: Array[String]): Unit = {
new T()
new T2()
//T3?
}
}
class T extends{val z = 10} with X with Y
class T2 extends X with Y {val z=10}
//class T3 extends{val z = 10} ??? Z with X with Y //????
class Z
trait X {
val z :Int
}
trait Y { this :X =>
println("Test: "+z)
}
Use:
class T3 extends {val z = 10} with Z with X with Y
Scala Language Specification 2.9, 5.1.6:
EarlyDefs ::= ‘{’ [EarlyDef {semi EarlyDef}] ‘}’ ‘with’
So an early definition always has to be followed by a with
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With