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

Failed matching when deconstructing Custom Composite Data #151

Open
algunion opened this issue Nov 9, 2022 · 1 comment
Open

Failed matching when deconstructing Custom Composite Data #151

algunion opened this issue Nov 9, 2022 · 1 comment

Comments

@algunion
Copy link

algunion commented Nov 9, 2022

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

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

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