I have the following defenition of an HList:
sealed trait HList
case class HCons[+HD, +TL <: HList](hd: HD, tl: TL) extends HList
case object HNil extends HList
I want to use the macros to be able to compile this code:
type KeyValue = (String, Int)
val hl2 = HCons("4" -> 4, HCons("5" -> 5, HCons("6" -> 6,HNil)))
val rhl2: HCons[KeyValue, HCons[KeyValue, HCons[KeyValue, Recorder.HNil.type]]] = reverse(hl2)
However, I cannot seem to keep all required type information. I get the following error:
[error] 291 | val rhl2: HCons[KeyValue, HCons[KeyValue, HCons[KeyValue, Recorder.HNil.type]]] = reverse(hl2)
[error] | ^^^^^^^^^^^^
[error] | Found: automl.Recorder.HCons[(String, Int), automl.Recorder.HList]
[error] | Required: automl.Recorder.HCons[KeyValue,
[error] | automl.Recorder.HCons[KeyValue,
[error] | automl.Recorder.HCons[KeyValue, automl.Recorder.HNil.type]
[error] | ]
[error] | ]
[error] one error found
I use the code below:
private def reverseLoop(e: Expr[HList], acc: Expr[HList])(using qctx:QuoteContext): Expr[HList] = {
import qctx.reflect._
e match {
case '{HCons($a:$H,$t)} =>
val nacc = '{HCons($a, $acc)}
reverseLoop(t, nacc)
case '{HNil} =>
acc
}
}
transparent inline def reverse(inline expr: HList): Any = {
${reverseLoop('expr, 'HNil)}
}
I Think the problem stems from the fact that I return a generic HList. How does one tackle this problem?
TIA