Last active
October 11, 2017 06:58
-
-
Save kmizu/0d44ef69550be10512f239a0549d675b to your computer and use it in GitHub Desktop.
TShow
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
case class Text(body: String) | |
object Text { | |
def pack(body: String): Text = Text(body) | |
} | |
trait Show[A] { | |
def show(a: A): String | |
} | |
object Show { | |
implicit object StringShow extends Show[String] { | |
override def show(a: String): String = a | |
} | |
implicit object IntShow extends Show[Int] { | |
override def show(a: Int): String = a.toString | |
} | |
} | |
trait TShow[A] { | |
def tshow(a: A): Text | |
} | |
object TShow { | |
implicit def aShow[A](implicit ev: Show[A]): TShow[A] = new TShow[A] { | |
def tshow(a: A): Text = Text.pack(ev.show(a)) | |
} | |
} | |
object Main { | |
def toText[A:TShow](a: A): Text = { | |
implicitly[TShow[A]].tshow(a) | |
} | |
def main(args: Array[String]): Unit = { | |
println(toText(1)) // IntShow | |
println(toText("FOO")) // StringShow | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment