init - Weird initialisation when override val met DelayedInit in Scala -
following code's output makes me confused.
code
object test1 { trait { val s: string = "a" println(s"s = $s") } abstract class b extends delayedinit { override def delayedinit(body: => unit) = { println("b " + "1" * 80) body println("b " + "2" * 80) } } abstract class c extends delayedinit { override val s: string = "c" override def delayedinit(body: => 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 { def s: string = "a" println(s"s = $s") } abstract class b extends delayedinit { override def delayedinit(body: => unit) = { println("b " + "1" * 80) body println("b " + "2" * 80) } } abstract class c extends delayedinit { override def s: string = "c" override def delayedinit(body: => 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
output
test1 -- new b begin s = 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 = 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
tried both scala 2.10.2 & 2.11.0-m3.
in understanding, test1 should same test2. here 2 questions:
- why delayedinit has been called twice in test1.c? there not other class inherits trait in class hierarchy
- why
s == null
in test1.c?
why delayedinit has been called twice in test1.c? there not other class inherits trait in class hierarchy
this looks bug, try reporting scala jira.
why
s == null
intest1.c
?
i'd it's because val
in
override val s: string = "c"
is backed internal variable , variable initialized during initializatino of c
. , because delayedinit
doesn't capture trait
s, initialization a
performed before delayedinit
, before c
, val s
initialized.
(it'd interesting if posted decompiled java code classes.)
Comments
Post a Comment