Skip to content

Instantly share code, notes, and snippets.

@rohinp
Created February 7, 2017 04:17
Show Gist options
  • Save rohinp/d978a005498723e20a80b4259ad48556 to your computer and use it in GitHub Desktop.
Save rohinp/d978a005498723e20a80b4259ad48556 to your computer and use it in GitHub Desktop.
def add[A: Num](a: A, b: A): A = implicitly[Num[A]].+(a, b)
trait Num[A] {
def +(a: A, b: A): A
def -(a: A, b: A): A
def *(a: A, b: A): A
def negate(a: A): A
def fromInteger(i: Int): A
}
implicit object NumInt extends Num[Int] {
def +(a: Int, b: Int): Int = a + b
def -(a: Int, b: Int): Int = a - b
def *(a: Int, b: Int): Int = a * b
def negate(a: Int): Int = -a
def fromInteger(i: Int): Int = i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment