JuliaServices / Match.jl

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

Wrong result for enum #56

Closed bliang26 closed 1 year ago

bliang26 commented 5 years ago

Hi, I am getting a wrong result when I try to match an enum. Please see the following example. Is there any work around? Thanks.

julia> @enum Fruit begin
       orange
       apple
       end

julia> fruit = orange
orange::Fruit = 0

julia> @match fruit begin
       apple  => "apple"
       orange => "orange"
       end
"apple"
thautwarm commented 5 years ago

@bliang26 In apple => "apple", the first apple is treated as a symbol to capture.

According to the Match.jl's documents, now you cannot reference variables to compare with the value to match.

And in MLStyle.jl, use &:

julia> using MLStyle

julia> @enum Fruit begin
              orange
              apple
              end
julia> fruit = orange
orange::Fruit = 0

julia> @match fruit begin
              &apple  => "apple"
              &orange => "orange"
       end
"orange"

What you want is the pin operators from Elixir language.

norru commented 4 years ago

Just had this problem, caused a bug which lost me hours to track down as I was incorrectly assuming this module would work as it would have in other languages.

As this is my most common use case, I won't be able to continue using this package.

thautwarm commented 4 years ago

@norru so try MLStyle.jl? :-)

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

julia> @enum Fruit begin
              orange
              apple
              end

julia> fruit = orange
orange::Fruit = 0

julia> @match fruit begin
              $apple  => "apple"
              $orange => "orange"
              end
"orange"