-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Pattern matching that supports equality #1059
Comments
Sorry if this is a duplicate, but I couldn't find this question in either the rust or rfc issues. |
Other languages which have pattern matching don't allow this. In Haskell, this code doesn't compile:
But this code does
IMO, pattern matching should be a form of destructuring, not running functions like equality, so, I don't think that this is a good feature to add. |
@Undeterminant As a counterexample to that, Erlang is another language which does. -module(equality).
-export([eq/2]).
eq(X, X) -> true;
eq(_, _) -> false. Since we're talking about pattern matching, I think equality is a pattern that could be detected. |
The issue with supporting equality in patterns in general is that its ambitious what kind of equality is meant - Does it require a |
This requires special casing I prefer the current, shadowing behaviour. |
I would argue that no pattern without a guard should ever desugar to a pattern with a guard, because patterns with a guard do not influence exhaustiveness checks and reachability checks for later match arms. That is, (Full disclaimer: I have a draft RFC on the "in a month or two" horizon that depends on this sort of distinction, because it proposes using match patterns to influence coherence checks involving dependent types.) That's not necessarily an argument against allowing |
I am strongly against implementing this. As said above, this generates a very subtle (and possibly expensive) equality check. Maybe this could make sense when restricted to |
Closing in favor of tracking issue #1059. |
@Centril your link seems to be wrong, it points to this same issue. |
Apparently I need some sleep... |
Hi all,
I was going through http://rustbyexample.com/flow_control/match/guard.html
and I tried to do https://gist.github.com/CaseyLeask/89d85dd213c618dac58d#file-guard-rs-L7 .
Is there a reason that this wouldn't be desirable?
Cheers,
Casey
The text was updated successfully, but these errors were encountered: