Skip to content

Instantly share code, notes, and snippets.

@lanceon
Last active December 20, 2015 16:59
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/6165350 to your computer and use it in GitHub Desktop.
Save lanceon/6165350 to your computer and use it in GitHub Desktop.
Scala traits composition and method overriding
object TraitsTest {
trait Base {
def func() = println("Base.func")
}
trait A extends Base {
override def func() = { super.func(); println("A.func") }
}
trait B extends Base {
override def func() = { super.func(); println("B.func") }
}
class MyClass1 extends A with B { println("Constructing MyClass1") }
class MyClass2 extends B with A { println("Constructing MyClass2") }
def main(args: Array[String]) {
(new MyClass1).func() // --> Constructing MyClass1, Base.func, A.func, B.func
(new MyClass2).func() // --> Constructing MyClass2, Base.func, B.func, A.func
}
}
@lanceon
Copy link
Author

lanceon commented Aug 6, 2013

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