Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Subtyping
object Subtyping {
trait Addable[A] {
def add(other: A): A
}
def combine[A <% Addable[A]](x: A, y: A): A =
x.add(y)
implicit def intToAddable(x: Int): Addable[Int] = new Addable[Int] {
override def add(other: Int) = x + other
}
implicit def stringToAddable(x: String): Addable[String] = new Addable[String] {
override def add(other: String) = x + other
}
def main(args: Array[String]): Unit = {
combine(1, 2)
combine("abc", "xyz")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.