Skip to content

Instantly share code, notes, and snippets.

@lanceon
Created November 21, 2014 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanceon/c658680b947ffbf860b0 to your computer and use it in GitHub Desktop.
Save lanceon/c658680b947ffbf860b0 to your computer and use it in GitHub Desktop.
Scala objects construction order
trait A {
println("constructing A")
val a = {
val x = value
println(s"calculating A.a = $x")
x
}
def value = {
println("A.value called")
0
}
def get = {
val x = a
println(s"get called = $x")
x
}
}
class B extends A {
println("constructing class B")
val default = {
println("calculating 'default' = 1")
1
}
override def value: Int = {
val x = default
println(s"B.value called = $x")
x
}
}
new B().get
@lanceon
Copy link
Author

lanceon commented Nov 21, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment