MLStyle.jl icon indicating copy to clipboard operation
MLStyle.jl copied to clipboard

Failed matching when deconstructing Custom Composite Data

Open algunion opened this issue 3 years ago • 1 comments

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

algunion avatar Nov 09 '22 13:11 algunion

@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?

thautwarm avatar Dec 09 '22 05:12 thautwarm