Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
object Subtyping {
trait Addable[A] {
def add(other: A): A
def combine[A <% Addable[A]](x: A, y: A): A =
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.