Skip to content

Instantly share code, notes, and snippets.

@mrdziuban
Last active January 3, 2018 23:50
Show Gist options
  • Save mrdziuban/1f029218f4170c977e5c11fbaa837d58 to your computer and use it in GitHub Desktop.
Save mrdziuban/1f029218f4170c977e5c11fbaa837d58 to your computer and use it in GitHub Desktop.
Simple shapeless foldLeft
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