Skip to content

Instantly share code, notes, and snippets.

@Khalian
Created October 4, 2018 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Khalian/8ac14ca7b5b4033473d2d2850a610282 to your computer and use it in GitHub Desktop.
Save Khalian/8ac14ca7b5b4033473d2d2850a610282 to your computer and use it in GitHub Desktop.
Type class reference
sealed trait Addable[T] {
def add(a: T, b: T): T
}
object AddableInstances {
implicit val intAddable = new Addable[Int] {
override def add(a: Int, b: Int): Int = a + b
}
}
def add[T: Addable](a: T, b: T): T = implicitly[Addable[T]].add(a, b)
import AddableInstances._
add(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment