Skip to content
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

Should we support interpolated _ in expression literal? #19

Open
ghost opened this issue May 7, 2019 · 1 comment
Open

Should we support interpolated _ in expression literal? #19

ghost opened this issue May 7, 2019 · 1 comment

Comments

@ghost
Copy link

ghost commented May 7, 2019

I like that we support expression literals (as a side effect of comparing values):

julia> @match :(5+5) begin
         :(5+5) => true
         _ => false
       end
true

But while we do accept _ in the struct construction syntax, we don't support them in the expression literal syntax:

julia> @match :(5+5) begin
         Expr(:call, [:+, _, _])=> true
         _ => false
       end
true

julia> @match :(5+5) begin
         :($_ + $_) => true
         _ => false
       end
ERROR: syntax: all-underscore identifier used as rvalue around /Users/nathan.daly/.julia/packages/Rematch/MQlXA/src/Rematch.jl:72

I think the error happens because after #5, we're trying to actually use the value in the _ variable in scope. But I think this should respect our other uses of _ as a rhs variable to mean a wildcard.

Same problem with these:

julia> @match :(5+5) begin
         :(a_ + b_) => true
         _ => false
       end
false

julia> @match :(5+5) begin
         :($a_ + $b_) => true
         _ => false
       end
ERROR: UndefVarError: a_ not defined
Stacktrace:
 [1] top-level scope at /Users/nathan.daly/.julia/packages/Rematch/MQlXA/src/Rematch.jl:72

It's a bit weird because the $ interpolation means something inside :() and also now means something inside @match. And the names of variables mean something different inside the expression literal than outside it. So i'm not really sure what the right behavior is here...

Fwiw, MacroTool's @match macro gets around this problem because they are always matching expressions by default, not values, so they just always coopt names w/ _ inside expression literals.


There's also this option which currently fails. It would feel wrong to me to make this match because of the potential for an actual _ inside the expression, but I guess this would be most similar to what MacroTools does:

julia> @match :(5+5) begin
         :(_ + _) => true
         _ => false
       end
false
@ghost ghost changed the title Should support interpolated _ in expression literal Should we support interpolated _ in expression literal? May 7, 2019
@gafter
Copy link

gafter commented May 16, 2023

I think this is just a bug. The pattern

  • :(a + b) should match the expression :(a + b) and nothing else
  • :($_ + $_) should match any binary addition
  • :($a + $b) should match any binary addition, and define the pattern variables a and b.
  • :($$a + $$b) should match an addition, where the left-hand-side corresponds to the value of the local variable a and the right-hand-side corresponds to the value of the local variable b. One $ is for escaping the :, and the other is for escaping the pattern syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant