Skip to content
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 macro in loop #59

Closed
eckofoid opened this issue May 17, 2020 · 1 comment
Closed

match macro in loop #59

eckofoid opened this issue May 17, 2020 · 1 comment

Comments

@eckofoid
Copy link

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
Copy link
Member

gafter commented Aug 3, 2023

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

@gafter gafter closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants