Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Last active August 29, 2015 14:26
Show Gist options
  • Save gigiigig/2386cbb924f985b8f882 to your computer and use it in GitHub Desktop.
Save gigiigig/2386cbb924f985b8f882 to your computer and use it in GitHub Desktop.
Add prin for HList, shows how to implement HList funtionalities
import shapeless._, record._, ops.record._ , syntax.singleton._, labelled._
object console extends App {
trait Print[T <: HList] {
def print(t: T): String
}
object Print {
implicit val phnil: Print[HNil] = new Print[HNil] {
def print(t: HNil): String = "HNil"
}
implicit def pcons[H, T <: HList](implicit pt: Print[T]) = new Print[H :: T] {
def print(t: H :: T): String =
s"(${t.head.toString} : ${t.head.getClass.getSimpleName}) :: ${pt.print(t.tail)}"
}
}
implicit class PrintOpsi[T <: HList](t: T) {
def print(implicit p: Print[T]) = p.print(t)
}
val l = 1 :: "ciao" :: false :: HNil
println(s"res: ${l.print}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment