Created
September 15, 2011 20:05
-
-
Save razie/1220320 to your computer and use it in GitHub Desktop.
OO type class
This file contains hidden or 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
//============== the OO version | |
trait Eq[A] { // the type trait | |
def sameAs(b:A) : Boolean // so many name clashes with eq/equals etc | |
} | |
case class Student (name:String) extends Eq[Student] { // classic OO implementation | |
override def sameAs(b:Student) = this.name == b.name | |
} | |
// someone actually using the Eq: A must be a subtype of Eq | |
def member[A <: Eq[A]] (a:A, container:List[A]) = | |
container.exists(_.sameAs(a)) | |
member (Student("John"), List(Student("John"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment