`@match` gives the incorrect results when types are passed in as values
jonniediegelman opened this issue · comments
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?
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.
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?
So can we close this one?
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.
I would just include an appropriate error message?
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.