-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Macro fails to match on more specific case: HList example #10358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It looks like the problem is with the second parameter of HCons. Let's take a slightly simpler example: import scala.quoted._
sealed trait HList
case class HCons[HD](hd: HD, tl: HList) extends HList
case object HNil extends HList
def showFirstTwoImpl(e: Expr[HList])(using Quotes): Expr[String] = {
e match {
case '{HCons($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString}
case _ => '{""}
}
}
transparent inline def showFirstTwo(inline xs: HList) = ${ showFirstTwoImpl('xs) } object Main:
@main
def run(): Unit =
println(showFirstTwo(HCons(123, HCons("abc", HNil)))) Running the code above prints "123abc" but if you redefine case class HCons[HD, TL](hd: HD, tl: TL) extends HList then it prints an empty string |
Using case class HCons[HD, TL](hd: HD, tl: TL) extends HList we can make it match by changing - case '{HCons($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString}
+ case '{HCons($h1: hd1, HCons($h2: hd2, $_ : tl))} => '{$h1.toString ++ $h2.toString} or - case '{HCons($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString}
+ case '{HCons[hd, HCons[sd, tl]]($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString} The issue with the original one is that we infer case '{HCons[Any, HCons[Any, Any]]($h1, HCons[Any, Any]($h2, $_))} => ... That will not match because the type parameters of |
I have gotten bitten by this again. On reviewing this example, the solution: - case '{HCons($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString}
+ case '{HCons($h1: hd1, HCons($h2: hd2, $_ : tl))} => '{$h1.toString ++ $h2.toString} now fails to compile in Scala version 3.2.1 with the following error:
The second solution seems to work. Is this a regression issue or need I do something else? Compiler explanation
|
Uh oh!
There was an error while loading. Please reload this page.
Minimized code
Output
Expectation
I expected the match to succeed. If I use the more general case that is commented out, the match occurs.
The text was updated successfully, but these errors were encountered: