Created
February 8, 2018 10:19
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
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