-
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
if let && bool #2411
Comments
Actually this seems to have been discussed many times:)
(BTW, if the condition does only a single pattern match, a workaround can simply be a match with a guard. I think that's why multiple match x {
Some(x) if x > 42 => foo,
_ => bar,
} |
Well, I had come upon #2260, and that's the reason why I wanted to just stay with the simple Actually, reading #2175 makes me think the syntax may be better like: if let Some(x) = y if x > 42 {
foo
} else {
bar
} This way 1/ the syntax is exactly the same as that of match guards, which is more consistent, and 2/ it avoids the issue of having to set an operator precedence to However it's then going back into the debate of #2260, and I hoped a reduced scope might help in getting at least something? Or maybe I should just wait for things to settle a bit and then @petrochenkov's implementation will help things move forward, despite the RFC being closed? |
Hey there. Me and @scottmcm wrote #2260. Since then #2046 has been merged and is getting implemented soon (by @est31). If you want to propose |
Overall, I think it'd likely be better to first RFC the About dealing with ambiguities, I think that in order to stay retro-compatible the only way might be to force parenthesizing the So I'd see it like this: if (let BINDING = EXPR1) && EXPR2 {
STMT3
} else {
STMT4
} This would evaluate The advantage of this syntax is that it could, later, if need arises, be extended to an arbitrary sequence of However, in order to not introduce unexpected things (like Additionally, depending on the current usage among crates, a warning could be put in when doing With this I think we should have make a run around the potential issues of syntax put forward by this proposal. |
There are some holidays in May, so I hope to write an |
I'm not sure about overloading I think
Is perhaps clear, but the semantics are a bit weird. It's much better than To be honest, we could use "where" for this:
Extends to if/else blocks:
or possibly even allowing different decompositions via
Another option if you prefer forcing users to declare their variables before use rather than needing to defer name resolution is something like "st" for "such that"
I feel like these more neatly communicate a separation of ideas as far as declaring bindings vs testing conditions on those bindings. While it's true the binding is itself somewhat a condition, personally I feel they're sufficiently different to warrant syntactic separation as well. The only major potential drawback I see to this approach over |
I agree with @LinearZoetrope that the combination of if (foo, bar) is (Some(x), Some(y)) where x > y { } |
The |
I can't say that the |
Closing this since #2497 was accepted. |
If I may, I think there's been a communication issue here: this issue should have been pinged when the eRFC was open. Now… it does look good, thanks! |
While they might sound similar, #2497 actually has no overlap with this issue:
Therefore I believe this issue should stay open. As a side note, we should consider including the change to |
@davidrolle #2497 already included |
First, there are currently 474 issues|PRs here that contain "if let", so I hope I'm not duplicating something already discussed.
This idea is quite close to #929, but instead of allowing to
&&
let
s, it'd allow to&&
with abool
.Basically, the idea looks like this:
This pattern is something that frequently occurred to me, and I'm finding it painful to always look for workarounds.
The constraints to this pattern would be:
if let Some((a, b)) = x && a > 42 && let Some(c) = b.unwrap()
for later (modulo Support && in if let expressions #929)What do you think about this idea?
The text was updated successfully, but these errors were encountered: