JuliaServices / Match.jl

Advanced Pattern Matching for Julia
https://juliaservices.github.io/Match.jl/latest/
Other
240 stars 22 forks source link

match macro in loop #59

Closed eckofoid closed 1 year ago

eckofoid commented 4 years ago

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 1 year ago

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