Skip to content

Instantly share code, notes, and snippets.

@uarun
Created January 14, 2014 04:45
Show Gist options
  • Select an option

  • Save uarun/8413175 to your computer and use it in GitHub Desktop.

Select an option

Save uarun/8413175 to your computer and use it in GitHub Desktop.
Type class technique as used in Scalaz 7 for method injection
package org.uarun.typeclasses
object Main extends ToDoubleOps {
def main(args: Array[String]) {
val x = 100
println(s"$x doubled is ${x.doubled}")
}
}
trait DoubleOps[A] {
val self: A
val m: Numeric[A]
def doubled: A = m.plus(self, self)
}
trait ToDoubleOps {
implicit def toDoubleOps[A: Numeric](a: A): DoubleOps[A] = new DoubleOps[A] {
val self: A = a
val m: Numeric[A] = implicitly[Numeric[A]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment