Skip to content

Instantly share code, notes, and snippets.

@rubenpieters
Created February 8, 2018 10:19
Show Gist options
  • Save rubenpieters/949525c2bd60bf6e321b75a465aeb65e to your computer and use it in GitHub Desktop.
Save rubenpieters/949525c2bd60bf6e321b75a465aeb65e to your computer and use it in GitHub Desktop.
trait Eq[A] {
val eq: String
}
trait Ord[A] {
val ord: String
}
implicit val eqInt: Eq[Int]
= new Eq[Int] { val eq = "eqInt"}
implicit val ordInt: Ord[Int]
= new Ord[Int] { val ord = "ordInt" }
implicit val eqString: Eq[String]
= new Eq[String] { val eq = "eqString" }
trait Sortable[A] {
def sortMe(l: List[A]): String
}
trait LowPrio {
implicit def sortEq[A](implicit ev: Eq[A]): Sortable[A]
= new Sortable[A] {
def sortMe(l: List[A]) = "sorted using " + ev.eq
}
}
object HighPrio extends LowPrio {
implicit def sortOrd[A](implicit ev: Ord[A]): Sortable[A]
= new Sortable[A] {
def sortMe(l: List[A]) = "sorted using " + ev.ord
}
}
import HighPrio._
def sort[A](l: List[A])(implicit ev: Sortable[A]): String
= ev.sortMe(l)
println(sort(List(1,2,3)))
// sorted using ordInt
println(sort(List("a","b","c")))
// sorted using eqString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment