Skip to content

Instantly share code, notes, and snippets.

@alexarchambault
Last active August 29, 2015 14:11
Show Gist options
  • Save alexarchambault/c104ecffcb6190a546d2 to your computer and use it in GitHub Desktop.
Save alexarchambault/c104ecffcb6190a546d2 to your computer and use it in GitHub Desktop.
Lazy - should not compile, stackoverflow
trait TC[T] {
def apply(t: T): String
}
object TC {
implicit def hnilTC: TC[HNil] =
new TC[HNil] {
def apply(l: HNil) = "HNil"
}
implicit def hconsTC[H, T <: HList](implicit
headTC: Lazy[TC[H]],
tailTC: Lazy[TC[T]]
): TC[H :: T] =
new TC[H :: T] {
def apply(l: H :: T) = s"${headTC.value(l.head)} :: ${tailTC.value(l.tail)}"
}
implicit def projectTC[F, G](implicit
gen: Generic.Aux[F, G],
tc: Lazy[TC[G]]
): TC[F] =
new TC[F] {
def apply(f: F) = tc.value(gen.to(f))
}
}
case class Bar(i: Int, s: String)
object Dummy extends App {
val barTC = implicitly[TC[Bar]]
Console.out.println(barTC(Bar(2, "a")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment