Skip to content

Instantly share code, notes, and snippets.

@ryandavidhartman
Created May 15, 2016 00:51
Show Gist options
  • Save ryandavidhartman/4f671393236e9baf1593c7333b4c4b2b to your computer and use it in GitHub Desktop.
Save ryandavidhartman/4f671393236e9baf1593c7333b4c4b2b to your computer and use it in GitHub Desktop.
trait A {
def foo(text: String) = printf("a says:" + text)
}
trait B extends A {
override def foo(text: String) = printf("b says:" + text)
}
trait C extends A {
override def foo(text: String) = printf("c says:" + text)
}
class D extends B with C
object main {
def main(args: Array[String]) {
val test = new D
test.foo("hi")
}
}
@ryandavidhartman
Copy link
Author

Scala allows multiple instantiation of traits, which allows for multiple inheritance by adding a distinction between the class hierarchy and the trait hierarchy. A class can only inherit from a single class, but can mix-in as many traits as desired. Scala resolves method names using a right-first depth-first search of extended 'traits', before eliminating all but the last occurrence of each module in the resulting list. So, the resolution order is: [D, C, A, B, A], which reduces down to [D, C, B, A].
Tcl allows multiple parent classes; the order of specification in the class declaration affects the name resolution for members using the C3 linearization algorithm.[10]

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