Why use simple val in non-finite classes

If the class is not final, it can be extended.

There are two possibilities for meaning: it can be redefined and must be lazy for it, it cannot be redefined and must be final.

If val is final, you can assume that all calculations on it will work through a class hierarchy. If val can be exceeded, you should declare it lazy so as not to break after expansion. You can leave val plain, and this does not guarantee that it will be distributed correctly.

What use cases involve using simple values?




Example of class initialization failure without lazy values

abstract class A { lazy val x1 : String = throw new Exception() val x2 : String = "mom" val x3 : String = x1 + ", " + x2 println("A: " + x3) } class B extends A { override lazy val x1: String = "hello" println("B: " + x3) } class C extends B { override val x2: String = "dad" println("C: " + x3) } 

testing:

 scala> new B A: hello, mom B: hello, mom res8: B = B@7e2bd615 

it works, but a further subclass has broken existing functionality

 scala> new C A: hello, null B: hello, null C: hello, null res5: C = C@52a53948 

setting lazy to x2 fixes the case:

 abstract class A { lazy val x1 : String = throw new Exception() lazy val x2 : String = "mom" val x3 : String = x1 + ", " + x2 println("A: " + x3) } class B extends A { override lazy val x1: String = "hello" println("B: " + x3) } class C extends B { override lazy val x2: String = "dad" println("C: " + x3) } 

right initialization order:

 scala> new C A: hello, dad B: hello, dad C: hello, dad res6: C = C@5e970110 
+3
scala lazy-evaluation final
Aug 23 2018-12-12T00:
source share
1 answer

Basically, you spin it.

The problem is not overriding, the problem is that you do not pay attention to the order in which things are initialized. There are ways to make it work, as well as ways to make it not work, and you have chosen the latter. It works:

 scala> class C extends { override val x2: String = "dad" } with B { | println("C: " + x3) | } defined class C scala> new C A: hello, dad B: hello, dad C: hello, dad res0: C = C@356e3aaf 
+3
Aug 23 '12 at 16:12
source share



All Articles