Last active
January 3, 2018 23:50
-
-
Save mrdziuban/1f029218f4170c977e5c11fbaa837d58 to your computer and use it in GitHub Desktop.
Simple shapeless foldLeft
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.{::, HList, HNil, Poly2} | |
import shapeless.ops.hlist.Prepend | |
import shapeless.record._ | |
import shapeless.syntax.singleton._ | |
val h = 1 :: "foo" :: true :: HNil | |
object withoutAnnotation extends Poly2 { | |
implicit def id[Acc <: HList, A, Res <: HList]( | |
implicit pp: Prepend.Aux[Acc, A :: HNil, Res]) = | |
at[Acc, A]((acc, a) => pp.apply(acc, a :: HNil)) | |
} | |
object withCaseAnnotation extends Poly2 { | |
implicit def id[Acc <: HList, A, Res <: HList]( | |
implicit pp: Prepend.Aux[Acc, A :: HNil, Res]): Case[Acc, A] = | |
at[Acc, A]((acc, a) => pp.apply(acc, a :: HNil)) | |
} | |
object withCaseAuxAnnotation extends Poly2 { | |
implicit def id[Acc <: HList, A, Res <: HList]( | |
implicit pp: Prepend.Aux[Acc, A :: HNil, Res]): Case.Aux[Acc, A, Res] = | |
at[Acc, A]((acc, a) => pp.apply(acc, a :: HNil)) | |
} | |
// does not compile | |
h.foldLeft(HNil)(withoutAnnotation) | |
// does not compile | |
h.foldLeft(HNil)(withCaseAnnotation) | |
// compiles | |
h.foldLeft(HNil)(withCaseAuxAnnotation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment