Created
May 18, 2010 12:08
-
-
Save pfleidi/404917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Similarity { | |
def isSimilar(x: Any): Boolean | |
def isNotSimilar(x: Any): Boolean = !isSimilar(x) | |
} | |
class TestClass(val member: Boolean) extends Similarity { | |
// method implementation with cast | |
def isSimilar(x: Any) = { x.asInstanceOf[TestClass].member == member } | |
} | |
object TraitTest { | |
def testSimilarity(arg0: Similarity, arg1: Similarity) { | |
println(arg0.isSimilar(arg1)) | |
} | |
} | |
object MainApp extends Application { | |
TraitTest.testSimilarity( | |
new TestClass(true), new TestClass(false) // false | |
) | |
TraitTest.testSimilarity( | |
new TestClass(true), new TestClass(true) // true | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A small example to show how traits work as a polymorphic type in scala: