Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. trait Eq[A] {
  2. val eq: String
  3. }
  4. trait Ord[A] {
  5. val ord: String
  6. }
  7.  
  8. implicit val eqInt: Eq[Int]
  9. = new Eq[Int] { val eq = "eqInt"}
  10. implicit val ordInt: Ord[Int]
  11. = new Ord[Int] { val ord = "ordInt" }
  12.  
  13. implicit val eqString: Eq[String]
  14. = new Eq[String] { val eq = "eqString" }
  15.  
  16. trait Sortable[A] {
  17. def sortMe(l: List[A]): String
  18. }
  19.  
  20. trait LowPrio {
  21. implicit def sortEq[A](implicit ev: Eq[A]): Sortable[A]
  22. = new Sortable[A] {
  23. def sortMe(l: List[A]) = "sorted using " + ev.eq
  24. }
  25. }
  26.  
  27. object HighPrio extends LowPrio {
  28. implicit def sortOrd[A](implicit ev: Ord[A]): Sortable[A]
  29. = new Sortable[A] {
  30. def sortMe(l: List[A]) = "sorted using " + ev.ord
  31. }
  32. }
  33.  
  34. import HighPrio._
  35.  
  36. def sort[A](l: List[A])(implicit ev: Sortable[A]): String
  37. = ev.sortMe(l)
  38.  
  39. println(sort(List(1,2,3)))
  40. // sorted using ordInt
  41. println(sort(List("a","b","c")))
  42. // sorted using eqString
Add Comment
Please, Sign In to add comment