-
Notifications
You must be signed in to change notification settings - Fork 39
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
Matching enum #126
Comments
A possible option is to report you that the global variable using MLStyle
MLStyle_AllowShadow = false
@enum Var left middle right
@match right begin
left => 0
middle => 1
right => 2
end
┌ Error: global variables used as capture patterns: left, middle, right.
└ @ Main xxx:xxx Could this suffice? |
I'm an unsophisticated user -- I don't understand why it gives
using MLStyle.AbstractPatterns: literal
MLStyle.is_enum(::Var) = true
MLStyle.pattern_uncall(e::Var, _, _, _, _) = literal(e) instead so I get the right answer instead of an error.
|
Thanks for this explanation. I understand your concerns, but unfortunately it's hard to achieve what you prefer. |
Could MLStyle do something like this? MLStyle.is_enum(::T) where {T<:Enum} = true
MLStyle.pattern_uncall(e::T, _, _, _, _) where {T<:Enum} = literal(e) It seems to work in this case julia> @match right begin
left => 0
middle => 1
right => 2
end
2 I think I understand your explanation. For other types that aren't known to MLStyle, like SumTypes.jl, it doesn't know whether I mean the existing name or a new name, and the "AllowShadow = false` suggestion would disambiguate. I think #127 would also avoid this accident, maybe that is a solution? |
MLStyle 0.5 will support |
This just bit me, I didn't realise this silently gave the wrong results for enums. If you can't make matching on an enum error, might I suggest adding the warning you have in the README to https://thautwarm.github.io/MLStyle.jl/latest/index.html as well as I went to check there first and didn't look at the README. |
Sometimes I want to match on an enum.
This gives the result
0
, but I want2
. As documented in pattern matching for Julia enums I can defineto get the right answer. But I'm nervous about using this. If I forget to define these methods, or define them in the wrong place so they're not executed yet, I'll get the wrong answer.
I would prefer to get either the right answer or an error rather than the wrong answer. What would be a good solution here?
The text was updated successfully, but these errors were encountered: