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 gives the incorrect results when types are passed in as values #189

Closed
jonniediegelman opened this issue Mar 13, 2024 · 8 comments
Closed

Comments

@jonniediegelman
Copy link

jonniediegelman commented Mar 13, 2024

Given the following result for matching on values

julia> x = 2;

julia> @match x begin
           1 => "One!"
           2 => "Two!"
           _ => "Many!"
       end
"Two!"

I'd expect the following to similarly match on values and output "I'm a Type{Float64}!":

julia> T = Float64;

julia> @match T begin
           Int => "I'm a Type{Int}!"
           Float64 => "I'm a Type{Float64}!"
           _ => "I don't know what I am!"
       end

Instead it outputs "I'm a Type{Int}!". I now know that the recommended way of doing this is

julia> @match T begin
           ::Type{Int} => "I'm a Type{Int}!"
           ::Type{Float64} => "I'm a Type{Float64}!"
           _ => "I don't know what I am!"
       end

but still the other version just silently gives the incorrect result and it can be pretty tricky to debug code that has this mistake in it. And it's still not really clear to me why the other version is wrong.

If it's not possible to get type-as-value version working, would it at least be possible to throw an error when a type is passed in as a value like this?

@jariji
Copy link
Contributor

jariji commented Mar 13, 2024

It's because in the first example those are literal patterns because they are 1 and 2, whereas Float64 is a capturing pattern because it's a symbol.

@jonniediegelman
Copy link
Author

jonniediegelman commented Mar 13, 2024

I see. So Float64 is interpreted as just some variable for the right side to use in this case. This does indeed happen without types:

julia> one = 1; two = 2;

julia> x = two;

julia> @match x begin
           one => "One!"
           two => "Two!"
           _ => "Many!"
       end
"One!"

Maybe an error could be thrown if two patterns are the "same" on the left side?

@jariji
Copy link
Contributor

jariji commented Mar 13, 2024

That's like #127

@ShalokShalom
Copy link

So can we close this one?

@thautwarm thautwarm pinned this issue Jun 1, 2024
@thautwarm
Copy link
Owner

I'd close this but pin it as it is a FAQ.
If anyone has a different idea, you could just have discussions here which is helpful for other users with this issue.

@ShalokShalom
Copy link

I would just include an appropriate error message?

@thautwarm
Copy link
Owner

I would just include an appropriate error message?

Do you have any design ideas? MLStyle can detect redundant patterns in some cases, but I'm not sure if I should mark things as intentional or unexpected.

@jariji
Copy link
Contributor

jariji commented Jun 1, 2024

Is there any benefit to allowing redundant cases (#127)?

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

4 participants