Skip to content

Instantly share code, notes, and snippets.

@izmailoff
Created August 3, 2014 13:41
Show Gist options
  • Save izmailoff/1f4688732dbb18bfd1ab to your computer and use it in GitHub Desktop.
Save izmailoff/1f4688732dbb18bfd1ab to your computer and use it in GitHub Desktop.
Trait linearization examples
// This was run in Scala REPL:
scala> trait A {
| println("CREATED A")
| def n: String
| }
defined trait A
scala>
scala> trait B extends A {
| println("CREATED B")
| override def n = "B"
| }
defined trait B
scala>
scala> (new A with B {}).n
CREATED A
CREATED B
res0: String = B
scala>
scala> (new B {}).n
CREATED A
CREATED B
res1: String = B
scala>
scala> ///-------------------
scala>
scala> trait C extends A {
| println("CREATED C")
| override def n = "C"
| }
defined trait C
scala>
scala> (new A with B with C {}).n
CREATED A
CREATED B
CREATED C
res2: String = C
scala>
scala> (new A with C with B {}).n
CREATED A
CREATED C
CREATED B
res3: String = B
scala>
scala> (new B with C {}).n
CREATED A
CREATED B
CREATED C
res4: String = C
scala>
scala> (new C with B {}).n
CREATED A
CREATED C
CREATED B
res5: String = B
scala>
scala> ///------------------
scala>
scala> trait D extends B {
| println("CREATED D")
| override def n = "D"
| }
defined trait D
scala>
scala> (new B with D {}).n
CREATED A
CREATED B
CREATED D
res6: String = D
scala>
scala> (new D with B {}).n
CREATED A
CREATED B
CREATED D
res7: String = D
scala>
scala> (new C with D {}).n
CREATED A
CREATED C
CREATED B
CREATED D
res8: String = D
scala>
scala> (new D with C {}).n
CREATED A
CREATED B
CREATED D
CREATED C
res9: String = C
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment