Last active
September 16, 2017 18:33
-
-
Save tomwadeson/164dd3e8bf49056a459d7d1a976f2594 to your computer and use it in GitHub Desktop.
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
package com.tomwadeson.show | |
import com.tomwadeson.Show.Showable | |
import scala.language.implicitConversions | |
trait Show[A] { | |
def show(a: A): String | |
} | |
object Show { | |
def apply[A: Show]: Show[A] = implicitly | |
def instance[A](f: A => String): Show[A] = (a: A) => f(a) | |
def print[A: Show](a: A): Unit = println(Show[A].show(a)) | |
implicit val showInt: Show[Int] = instance(_.toString) | |
implicit val showString: Show[String] = instance(identity) | |
implicit def showList[A: Show]: Show[List[A]] = | |
instance(as => as.map(Show[A].show).mkString(", ")) | |
type Showable = TcBox[Show] | |
implicit val showShowable: Show[Showable] = | |
Show.instance(s => s.evidence.show(s.value)) | |
} | |
sealed trait TcBox[Tc[_]] { | |
type A | |
val value: A | |
val evidence: Tc[A] | |
} | |
object TcBox { | |
final case class MkTcBox[Tc[_], B](value: B)(implicit val evidence: Tc[B]) extends TcBox[Tc] { type A = B } | |
implicit def mkTcBox[Tc[_], A : Tc](a: A): TcBox[Tc] = MkTcBox(a) | |
} | |
object Main extends App { | |
Show.print(List(1, 2, "3", 4, 5): List[Showable]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment