I am using Scala 3 to implement the FromExpr type so I can implement a macro. I looked at the dotty code for inspiration, but I have a compilation error that I cannot solve. The minimal code snippet is in this scastie session.
I have a recursive structure (HList) defined as follows:
sealed trait IMap
case object IEmpT extends IMap
case class ICons[K, V, T <: IMap](key: K, value: V, tail: T) extends IMap
type IEmpT = IEmpT.type
As per the source code I split the reconstruction into 3 methods. One for the general IMap
, one for IEmpT
and one for ICons
. The error I get is:
value value is not a member of scala.quoted.Expr[Playground.From.ICons[K, V, T]].
An extension method was tried, but could not be fully constructed:
x$2.value[Playground.From.ICons[K, V, T]](x)(
Playground.From.IConsFromExpr[K, V, T]
)
The correct implicit function is being called but I have some problem that seems to prevent the instantiation of the recursive structure. I would have only expected such an error at the call site. The implicit function in question is:
inline given IConsFromExpr[ K: Type : FromExpr, V: Type : FromExpr, T <: IMap](using Type[T], FromExpr[T]): FromExpr[ICons[K,V,T]] with {
inline def unapply(x: Expr[ICons[K,V,T]])(using Quotes) = x match {
case '{ICons[K,V,T](${Expr(k)}, ${Expr(v)}, $tl) } =>
Some(ICons(k,v,tl.valueOrAbort))
case _ => None
}
}
and the error occurs in the second case of this function:
inline given IMapFromExpr[K, V, T](using Type[K], Type[V], Type[T], FromExpr[K], FromExpr[V], FromExpr[T]):
FromExpr[_ <: IMap] with {
inline def unapply(x: Expr[_ <: IMap])(using Quotes) = x match {
case '{ $x: IEmpT } => x.value
case '{ $x: ICons[K, V, T] } => x.value
case _ => None
}
}
I have tried some variants including the equivalent quote:
case '{ICons[K,V,T](${Expr(k)}, ${Expr(v)}, ${Expr(tl)}) } =>
Some(ICons(k,v,tl))
but have had no success. Can anyone see what I am doing wrong?
TIA