I am trying to take an HList
type that can itself have elements that are HList
s and flatten it. My initial attempt was:
type Concat[Xs <: HList, Ys <: HList] <: HList = Xs match {
case HNil.type => Ys
case HCons[x1, xs1] => HCons[x1, Concat[xs1, Ys]]
}
type Flatten[Xs <: HList] <: HList = Xs match {
case HCons[HCons[h1,t1], t] => Concat[Flatten[HCons[h1,t1]], Flatten[t]]
case HCons[h, t] => HCons[h, Flatten[t]]
case HNil.type => HNil.type
}
summon[Flatten[HCons[
HCons[1,HCons[2, HNil.type]],
HCons[3, HNil.type]
]
] =:= HCons[1,HCons[2,HCons[3,HNil.type]]] ]
The above Flatten
does not work. After some experimentation I found that case HCons[h, t] =>
subsumes the more specific case HCons[HCons[h1,t1], t] =>
- so the h
will match any type including an HCons
.
I have considered trying to enforce type inequality in the match cases but this is out of my league.
Does anyone know how do do this or know were I can get info on how to do this?
TIA