Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird initialisation when override val met DelayedInit in Scala
    text
    copied!<p>Following code's output makes me confused.</p> <h2>code</h2> <pre><code>object Test1 { trait A { val s: String = "A" println(s"s = $s") } abstract class B extends A with DelayedInit { override def delayedInit(body: =&gt; Unit) = { println("B " + "1" * 80) body println("B " + "2" * 80) } } abstract class C extends A with DelayedInit { override val s: String = "C" override def delayedInit(body: =&gt; Unit) = { println("C " + "1" * 80) body println("C " + "2" * 80) } } def main { println("-- new B begin") new B { println("new B context") } println("-- new B end") println("-- new C begin") new C { println("new C context") } println("-- new C end") } } object Test2 { trait A { def s: String = "A" println(s"s = $s") } abstract class B extends A with DelayedInit { override def delayedInit(body: =&gt; Unit) = { println("B " + "1" * 80) body println("B " + "2" * 80) } } abstract class C extends A with DelayedInit { override def s: String = "C" override def delayedInit(body: =&gt; Unit) = { println("C " + "1" * 80) body println("C " + "2" * 80) } } def main { println("-- new B begin") new B { println("new B context") } println("-- new B end") println("-- new C begin") new C { println("new C context") } println("-- new C end") } } println("Test1") Test1.main println("Test2") Test2.main </code></pre> <h2>output</h2> <pre><code>Test1 -- new B begin s = A B 11111111111111111111111111111111111111111111111111111111111111111111111111111111 new B context B 22222222222222222222222222222222222222222222222222222222222222222222222222222222 -- new B end -- new C begin s = null C 11111111111111111111111111111111111111111111111111111111111111111111111111111111 C 22222222222222222222222222222222222222222222222222222222222222222222222222222222 C 11111111111111111111111111111111111111111111111111111111111111111111111111111111 new C context C 22222222222222222222222222222222222222222222222222222222222222222222222222222222 -- new C end Test2 -- new B begin s = A B 11111111111111111111111111111111111111111111111111111111111111111111111111111111 new B context B 22222222222222222222222222222222222222222222222222222222222222222222222222222222 -- new B end -- new C begin s = C C 11111111111111111111111111111111111111111111111111111111111111111111111111111111 new C context C 22222222222222222222222222222222222222222222222222222222222222222222222222222222 -- new C end </code></pre> <p>Tried both scala 2.10.2 &amp; 2.11.0-M3.</p> <p>In my understanding, Test1 should be the same as Test2. Here is two questions:</p> <ul> <li>Why DelayedInit has been called twice in Test1.C? There is not other class inherits the trait in the class hierarchy</li> <li>Why <code>s == null</code> in Test1.C?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload