Skip to content

Instantly share code, notes, and snippets.

@jroesch
Last active August 29, 2015 14:21
Show Gist options
  • Save jroesch/a72b5d51ad0ee4a0da1a to your computer and use it in GitHub Desktop.
Save jroesch/a72b5d51ad0ee4a0da1a to your computer and use it in GitHub Desktop.
trait Convert[A, B] {
def convert(a : A) : B
}
object Convert {
implicit def convertCharInt = new Convert[Char, Int] {
def convert(c: Char): Int = c.toInt
}
implicit def convertIntChar = new Convert[Int, Char] {
def convert(i: Int): Char = i.toChar
}
}
implicit class ConvertSyntax[A](a: A) {
def convert[B]()(implicit ev: Convert[A, B]): B = ev.convert(a)
}
object Foo {
val x : Char = 1.convert()
val y : Int = 'c'.convert()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment