Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment