Scala: val initialization on class extension

How to pre-initialize val and extend a class in Scala?

eg:.

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) } 
+4
source share
2 answers

Using:

 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 for an early definition should always follow with

+5
source

Check here for some programming examples in the scala book

Scala: usage example for early definition / early initializer / pre-initialized fields

0
source

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


All Articles