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

Match with already defined variables (ie constants) #5

Open
programmeroftheeve opened this issue Aug 29, 2018 · 6 comments
Open

Match with already defined variables (ie constants) #5

programmeroftheeve opened this issue Aug 29, 2018 · 6 comments

Comments

@programmeroftheeve
Copy link

It should be able to match with constants.

Example:

using Rematch
module Mod
    const VALUE = 0
end
import .Mod

const FOO = 1
const BAR = 2
const FOOBAR = 3
const BAR_FOO = 4

x = FOO

y = @match x begin
    Mod.VALUE => :mod_value
    FOO => :foo
    BAR => :bar
    FOOBAR => :foobar
    BAR_FOO => :barfoo
    _ => nothing
end

@assert y == :foo
@mbravenboer
Copy link
Contributor

This sounds useful, but I'm not entirely sure that it can be implemented in a macro. We'd need to be able to reflect over what identifiers are in scope. Maybe @jamii can comment?

We could also introduce a specific syntax that identifies this, which would be a little more cumbersome to the user.

@jamii
Copy link
Contributor

jamii commented Sep 7, 2018

Basing it on scope would mean that compile order affects the results, which is probably unpleasant.

$FOO or ==(FOO) might work for syntax. Or you could match MacroTools and use _foo for variables.

@mbravenboer
Copy link
Contributor

Thanks @jamii , makes sense.

@mbravenboer mbravenboer changed the title Match with all ready defined variables (ie constants) Match with already defined variables (ie constants) Oct 22, 2018
@ghost
Copy link

ghost commented Oct 23, 2018

I agree with Jamie that the dollar-sign syntax would seem to make sense here: you're simply using the name to refer to the value you want to match on. So I think interpolating is a good metaphor here.

It also matches what BenchmarkTools does for their macros:

@btime inv($A);           # we interpolate the global variable A with $A
@benchmark [i*i for i in $A]

https://github.com/JuliaCI/BenchmarkTools.jl#quick-start
https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions

@mbravenboer mbravenboer assigned ghost Oct 23, 2018
@ghost
Copy link

ghost commented Dec 21, 2018

This was mostly fixed by #11.

@btaidm's original example can be matched like this:

julia> using Rematch

julia> module Mod
           const VALUE = 0
       end
>> Main.Mod

julia> begin
         const FOO = 1
         const BAR = 2
         const FOOBAR = 3
         const BAR_FOO = 4
       end
>> 4

julia> x = FOO
>> 1

julia> y = @match x begin
          $(Mod.VALUE) => :mod_value
          $FOO => :foo
          $BAR => :bar
          $FOOBAR => :foobar
          $BAR_FOO => :barfoo
          _ => nothing
       end
>> :foo

julia> @assert y == :foo

@ghost
Copy link

ghost commented Dec 21, 2018

I'm leaving the Issue open because it still doesn't support interpolating splatted patterns, such as this:

julia> let arr=[10,20,30],  x=[1,2,3, 10,20,30, 5]
         @match x begin
               # splatting existing values
               [fronts..., $(arr...), back] => [fronts..., back]
         end
       end

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

3 participants