thautwarm / MLStyle.jl

Julia functional programming infrastructures and metaprogramming facilities
https://thautwarm.github.io/MLStyle.jl/latest/
MIT License
404 stars 38 forks source link

Failed matching when deconstructing Custom Composite Data #151

Open algunion opened 2 years ago

algunion commented 2 years ago

Initially, I used a custom type related to my project, but after repeated failures of the matcher, I tried the tutorial examples - to make sure I was not doing something wrong.

Specifically, the evaluation of the following snippet goes wrong:

@data Color begin
    RGB(r::Int, g::Int, b::Int)
    Gray(Int)
end

@as_record RGB
@as_record Gray

color_to_int(x) = @match x begin
    RGB(;r, g, b) => 16 + b + 6g + 36r
    Gray(i)       => 232 + i
end

When calling RGB(200, 0, 200) |> color_to_int I get an error complaining about matching not being exhaustive.

I redefined the expression in the following way:

color_to_int(x) = @match x begin
    RGB(;r, g, b) => 16 + b + 6g + 36r
    Gray(i)       => 232 + i
    _ => 10
end

Now, the execution of RGB(200, 0, 200) |> color_to_int runs successfully - however, the output is 10.

It seems that the deconstruction of RGB(200, 0, 200) fails to be recognized as a valid RGB(;r, g, b) pattern.

I am using Julia 1.8.2 with MLStyle v0.4.15 inside Pluto v0.19.9.

matching_not_working

thautwarm commented 1 year ago
@data Color begin
    RGB(r::Int, g::Int, b::Int)
    Gray(Int)
end

@as_record RGB
@as_record Gray

color_to_int(x) = @match x begin
    RGB(;r, g, b) => 16 + b + 6g + 36r
    Gray(i)       => 232 + i
end

Sorry, using this snippet gives me RGB(200, 0, 200) |> color_to_int == 7416.

Could you please provide versioninfo() using your test environment?