Created
January 14, 2014 04:45
-
-
Save uarun/8413175 to your computer and use it in GitHub Desktop.
Type class technique as used in Scalaz 7 for method injection
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
| 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