-
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
Macroless mapping of patterns -> bool #2153
Comments
This looks to me like Bikeshed: Contextual keyword let is_a_123: bool = example matches Example::A(123, ..);
if example matches Example::A(.., "alex") || always_do_it {}
examples.filter(|t| !(*t matches Example::A(123, ..))); Or |
Hmmm, yes I suppose since the assignment operator has the least precedence it would seem like that. That's a big problem with the proposal, thanks for pointing that out.
I totally agree. But I didn't suggest this as I thought adding a new keyword to the language would be too difficult. The strength of enhancing |
I suppose the question now is: Can we do this in a backwards compatible way / without adding a new keyword? Could the let is_a_123: bool = example match Example::A(123, ..);
if example match Example::A(.., "alex") || always_do_it {}
examples.filter(|t| !(*t match Example::A(123, ..))); |
The problem with reusing the same syntactical pattern is that it's very weird for the refutability of a pattern to change the type of the |
See #929 (comment) |
Ultimately I believe we need a better solution to the over-verbose: if let $pattern = $expr { true } else { false } I'm not sure this issue is helping though, as it isn't a good enough proposal and isn't going anywhere. |
I while ago I submitted #163 for adding a |
Indeed, and a macro/if-else is what I use now for this. This was about getting the functionality into the language without macros or verbosity. This is something I still feel is worthwhile. |
Linking #2260 |
The current
if let $pattern = $expr {}
functionality is great when you're de-structuring parts of the pattern for use in the if-scope. However, when matching without that need it can feel quite limited. I would suggest allowing macroless evaluation of certain pattern matches ->bool
.Current State
Example compile error:
This makes sense when you can reference part of the pattern within the if-scope, but less so when it's a straight forward match.
The macroless way of mapping a pattern match to a boolean can currently be a little ugly.
New functionality
How about we allow pattern matches without a variable binding to be evaluated as bools.
This would mean the above examples would start working / could be more readable.
Its worth noting explicitly that I would expect patterns with variable bindings to not be allowed
This means this functionality would have a clear distinction from the current
if let $pattern = $expr {}
usage.Working example
I'd like this as macroless functionality built into the language. However, I can provide a simple macro for a working example.
These are not nice, but show the functionality working as close as we can currently get.
Comments
I think this would make a good addition to the current
if let
behaviour. So please fill me in on all the obvious stuff I've missed!The text was updated successfully, but these errors were encountered: