Skip to content

Quoted pattern matching does not work when quote is in a val. #16543

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

Open
smarter opened this issue Dec 16, 2022 · 3 comments
Open

Quoted pattern matching does not work when quote is in a val. #16543

smarter opened this issue Dec 16, 2022 · 3 comments
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:enhancement

Comments

@smarter
Copy link
Member

smarter commented Dec 16, 2022

Compiler version

3.3.0-RC1-bin-20221215-ef653b6-NIGHTLY

Minimized code

After inlining, pat(3) is replaced by "ok" as expected in the following code:

import scala.quoted.*

inline def pat(inline e: Int): String = ${patImpl('e)}

private def patImpl(e: Expr[Int])(using Quotes): Expr[String] = {
  e match
    case '{ 3 } => '{"ok"}
    case _ => '{"other"}
}
def test = pat(3)

But if I put the quote in a pattern, then I get "other" instead:

private def patImpl(e: Expr[Int])(using Quotes): Expr[String] = {
  val Expr = '{ 3 }
  e match
    case Expr => '{"ok"}
    case _ => '{"other"}
}

Expectation

I think ideally both code snippets should behave the same. My usecase is that I'd like to check if some expression is already present in the input, and if not output what code should be generated (cf https://contributors.scala-lang.org/t/scala-3-macro-annotations-and-code-generation/6035), so I'd like to write the Expr once and use it both for pattern matching and as an output.

@smarter smarter added itype:bug area:metaprogramming:quotes Issues related to quotes and splices labels Dec 16, 2022
@hmf
Copy link

hmf commented Dec 17, 2022

Related to #16533?

@nicolasstucki
Copy link
Contributor

The limitation here is that the match becomes a e == Expr. Currently, the expression equality only checks if the trees are preferentially equal. We miss the context to be able to compare them semantically.

We provide the users with the matches to this operation

private def patImpl(e: Expr[Int])(using Quotes): Expr[String] = {
  val Expr = '{ 3 }
  if e.matches(Expr) then '{"ok"}
  else '{"other"}

Another way we could achieve it is to support splicing expressions into (as opposed to out of) a quoted pattern.

@smarter
Copy link
Member Author

smarter commented Dec 19, 2022

I see, thanks! Maybe we could warn on use of == on Expr to let the user know about .matches?

smarter added a commit to dotty-staging/dotty that referenced this issue Jan 3, 2023
smarter added a commit to dotty-staging/dotty that referenced this issue Jan 5, 2023
@Kordyjan Kordyjan modified the milestone: Future versions Jan 13, 2023
@nicolasstucki nicolasstucki removed their assignment Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:enhancement
Projects
None yet
Development

No branches or pull requests

4 participants