Last active
August 29, 2015 14:21
-
-
Save jroesch/a72b5d51ad0ee4a0da1a to your computer and use it in GitHub Desktop.
This file contains 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
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