JuliaServices/Match.jl

match macro in loop

eckofoid opened this issue · 1 comments

I don't understand the following behavior:

julia> @enum State beg init final stop pass
julia> for state in instances(State)
if state == beg; println("start")
elseif state == init; println("initiate")
elseif state == final; println("terminate")
elseif state == stop; println("end")
else println("never")
end
@match state begin
beg => println("111")
init => println("222")
final => println("333")
stop => println("444")
never => println("555")
end
end

Output:
start
111
initiate
111
terminate
111
end
111
never
111

gafter commented

A pattern that is a simple name matches any input and binds the input to that name. If you want a name to be treated as an expression, you need to escape it. In version 2 of this package, here is the behavior:

julia> Pkg.status()
      Status `~/.julia/environments/v1.6/Project.toml`
  [7eb4fadd] Match v2.0.0

julia> @enum State beg init final stop pass

julia> for state in instances(State)
           if state == beg; println("start")
           elseif state == init; println("initiate")
           elseif state == final; println("terminate")
           elseif state == stop; println("end")
           else println("never")
           end
           @match state begin
               $beg => println("111")
               $init => println("222")
               $final => println("333")
               $stop => println("444")
               never => println("555")
           end
       end
start
111
initiate
222
terminate
333
end
444
never
555