-
Notifications
You must be signed in to change notification settings - Fork 6
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
Comments
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. |
Basing it on scope would mean that compile order affects the results, which is probably unpleasant.
|
Thanks @jamii , makes sense. |
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:
https://github.com/JuliaCI/BenchmarkTools.jl#quick-start |
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 |
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 |
It should be able to match with constants.
Example:
The text was updated successfully, but these errors were encountered: