I use implicit def to create a recursive HList type to match several types of higher HList types. I am very inspired by this post .
This code works fine:
sealed trait HList { type Plus[L <: HList] <: HList } class HNil extends HList { type Plus[L <: HList] = L def ::[T](v: T) = HCons(v, this) } case class Appender[L1 <: HList, L2 <: HList, R <: HList](fn: (L1, L2) => R) { def apply(l1: L1, l2: L2) = fn(l1, l2) } object HNil extends HNil object HList { def ++[L1 <: HList, L2 <: HList](l1: L1, l2: L2)(implicit f: Appender[L1, L2, L1#Plus[L2]]): L1#Plus[L2] = f(l1, l2) implicit def nilAppender[L <: HList]: Appender[HNil, L, L] = Appender((v: HNil, l: L) => l) implicit def consAppender[T, L1 <: HList, L2 <: HList, R <: HList](implicit f: Appender[L1, L2, R]): Appender[HCons[T, L1], L2, HCons[T, R]] = { Appender[HCons[T, L1], L2, HCons[T, R]]((l1: HCons[T, L1], l2: L2) => HCons(l1.head, f(l1.tail, l2))) } } case class HCons[T, U <: HList](head: T, tail: U) extends HList { type Plus[L <: HList] = HCons[T, U#Plus[L]] def ::[V](v: V) = HCons(v, this) } import HList._ val hlist1 = 2.0 :: "hi" :: HNil val hlist2 = 1 :: HNil val sum = ++(hlist1, hlist2) println("last element : " : + sum.tail.tail.head) // prints last element : 1"
Now I donβt know why, but if I try to add the ++ method to HCons , which simply calls the existing HList.++ method, this DOES NOT work:
case class HCons[T, U <: HList](head: T, tail: U) extends HList { type Plus[L <: HList] = HCons[T, U#Plus[L]] def ::[V](v: V) = HCons(v, this) def ++[L2 <: HList](l2: L2) = HList.++(this,l2) }
I get this compilation error:
could not find implicit value for parameter f: Appender[HCons[T,U],L2,HCons[T,U]#Plus[L2]]
Because HCons is a subtype of HList , like the L1 type defined by HList. ++, I thought everything was in order.
I tried this, but this does not work better:
implicit def consAppender[T, L1 <: HList, L2 <: HList, L3, R <: HList](implicit f: Appender[L1, L2, R], ev: L3 <:< HCons[T, L1]): Appender[HCons[T, L1], L2, HCons[T, R]] = { Appender[HCons[T, L1], L2, HCons[T, R]]((l1: L3, l2: L2) => HCons(l1.head, f(l1.tail, l2))) }
What did I miss?
Thanks:)