Skip to content

Instantly share code, notes, and snippets.

@razie
Created September 15, 2011 20:05
Show Gist options
  • Save razie/1220320 to your computer and use it in GitHub Desktop.
Save razie/1220320 to your computer and use it in GitHub Desktop.
OO type class
//============== 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