Skip to content

Instantly share code, notes, and snippets.

@kmizu
Last active October 11, 2017 06:58
Show Gist options
  • Save kmizu/0d44ef69550be10512f239a0549d675b to your computer and use it in GitHub Desktop.
Save kmizu/0d44ef69550be10512f239a0549d675b to your computer and use it in GitHub Desktop.
TShow
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