MacroTools.jl icon indicating copy to clipboard operation
MacroTools.jl copied to clipboard

Some inconsistencies when using Or matching

Open KristofferC opened this issue 2 years ago • 1 comments

Typically, if @capture(expr, match_1) matches you would expect @capture(expr, match_1 | match_2) to always also match. This is however not the case. For example:

julia> @capture(:(begin; x = 2;y=3; end), begin body_ end)
true

julia> body
quote
    #= REPL[2]:1 =#
    x = 2
    #= REPL[2]:1 =#
    y = 3
end

matches, but the following

julia> @capture(:(begin; x = 2;y=3; end), begin body_ end | for i_ in iter_ forbody_ end)
false

does not. In addition, it the expression in the block only contains one expression, there will be a match:

julia> @capture(:(begin; x = 2; end), begin body_ end | for i_ in iter_ forbody_ end)
true

julia> body
:(x = 2)

This feels inconsistent to me.

KristofferC avatar May 31 '23 09:05 KristofferC

Agreed that it's inconsistent. Not sure which way we should resolve it though.

julia> @capture(:(begin; x = 2;y=3; end), begin body_ end)
true

felt surprising to me, but at the same time I can kinda see the reasoning behind it. I'm a bit worried that if we changed that to false, we'd break people's code somewhere, and might learn the original reason for this behaviour.

So that leaves us with making

julia> @capture(:(begin; x = 2;y=3; end), begin body_ end | for i_ in iter_ forbody_ end)
false

true. Not a great fan, but it's probably safer.

cstjean avatar Jun 15 '23 12:06 cstjean