Last active
August 29, 2015 14:26
-
-
Save gigiigig/2386cbb924f985b8f882 to your computer and use it in GitHub Desktop.
Add prin for HList, shows how to implement HList funtionalities
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
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