Created
October 4, 2018 19:21
-
-
Save Khalian/8ac14ca7b5b4033473d2d2850a610282 to your computer and use it in GitHub Desktop.
Type class reference
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
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