Hey folks. I’ve been trying to figure out how to use MLStyle.Modules.AST.@capture
for doing the same thing as MacroTools.@capture(ex, fn_(args__; kws__) | fn_(args__))
However, I can’t seem to come up with the syntax that does what |
does in MacroTools.@capture
. I assumed ||
would work since it seems to be used in regular MLStyle patterns, but it doesn’t seem like it:
In [20]: let ex = :(somefunction(param1, 666; a=5))
@capture($fn($(args...);$(kw...)) || $fn($(args...)), ex)
end
ERROR: matching non-exhaustive, at #= REPL[20]:2 =#
Both of those patterns work separately though, but only if the expression matches exactly:
In [24]: let ex = :(somefunction(param1, 666; a=5))
@capture($fn($(args...);$(kw...)), ex)
end
Dict{Symbol, Any} with 3 entries:
:args => Any[:param1, 666]
:fn => :somefunction
:kw => Any[:($(Expr(:kw, :a, 5)))]
In [25]: let ex = :(somefunction(param1, 666))
@capture($fn($(args...)), ex)
end
Dict{Symbol, Any} with 2 entries:
:args => Any[:param1, 666]
:fn => :somefunction
So eg. the pattern with keywords won’t match a call that doesn’t have them
In [26]: let ex = :(somefunction(param1, 666))
@capture($fn($(args...);$(kw...)), ex)
end
ERROR: matching non-exhaustive, at #= REPL[26]:2 =#
Seems a bit inconvenient that @capture
throws if it gets an expression that doesn’t match the template.