Last active
August 29, 2015 14:11
-
-
Save alexarchambault/c104ecffcb6190a546d2 to your computer and use it in GitHub Desktop.
Lazy - should not compile, stackoverflow
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
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