Skip to content

Instantly share code, notes, and snippets.

@drostron
Last active December 26, 2015 04:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drostron/7093230 to your computer and use it in GitHub Desktop.
Save drostron/7093230 to your computer and use it in GitHub Desktop.
object TypeClassExample {
trait UnicodeOrdered[T] {
def ≤(i:T,j:T): Boolean
}
object UnicodeOrdered {
implicit object UnicodeOrderedInt extends UnicodeOrdered[Int] {
def ≤(i:Int,j:Int): Boolean = i <= j
}
implicit object UnicodeOrderedLong extends UnicodeOrdered[Long] {
def ≤(i:Long,j:Long): Boolean = i <= j
}
}
object UnicodeOrderedOps {
implicit class EnrichedUnicodeOrderedOps[T](content: T)(implicit m:UnicodeOrdered[T]) {
def ≤(v:T) = m.≤(content,v)
}
}
}
scala> import TypeClassExample._, UnicodeOrderedOps._
import TypeClassExample._
import UnicodeOrderedOps._
scala> 1 ≤ 2
res0: Boolean = true
scala> 1L ≤ 2
res1: Boolean = true
scala> 1.0 ≤ 2
<console>:14: error: could not find implicit value for parameter m: misc.TypeClassExample.UnicodeOrdered[AnyVal]
1.0 ≤ 2
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment