Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upeRFC: if- and while-let-chains, take 2 #2497
Conversation
Centril
added
T-lang
I-nominated
labels
Jul 13, 2018
Ixrec
reviewed
Jul 13, 2018
text/0000-if-let-chains.md Outdated
This comment has been minimized.
This comment has been minimized.
|
I am very for this change. This syntax feels natural and intuitive, and it is something I've pined for. My remarks are:
|
This comment has been minimized.
This comment has been minimized.
Thank you for the data point and the remarks. I think the primary motivation for also changing That said, if we consider iterating over multiple sources that produce elements in a zip-like fashion (but not strictly
I'm personally undecided on adding |
kennytm
reviewed
Jul 13, 2018
text/0000-if-let-chains.md Outdated
text/0000-if-let-chains.md Outdated
Centril
added some commits
Jul 13, 2018
This comment has been minimized.
This comment has been minimized.
|
@aturon has asked me to clarify what exactly is being proposed to be accepted with this RFC. So here goes... The precedent set by closing RFCs #2443, #2441, and #2429 is that we don't accept RFCs reserving syntax without accepting a feature along with it. Therefore, to be consistent with this policy, and for the purposes of making So the idea with this RFC is that we accept the full feature now (the idea of chaining...), but leave finalizing the syntax itself unresolved-ish as we did with #2071 ( After the Rust 2018 has shipped, and when we have time, we can then implement the proposed syntax and experiment with it on nightly. The syntax will still have to be finalized by some other decision, such as with another RFC or on the tracking issue. |
oli-obk
reviewed
Jul 13, 2018
text/0000-if-let-chains.md Outdated
text/0000-if-let-chains.md Outdated
text/0000-if-let-chains.md Outdated
text/0000-if-let-chains.md Outdated
text/0000-if-let-chains.md Outdated
text/0000-if-let-chains.md Outdated
Centril
added some commits
Jul 13, 2018
This comment has been minimized.
This comment has been minimized.
|
My opinion hasn't changed since #2260 (comment). We should aim for supporting a convenient non-exhaustive pattern-matching expressions in general (
That's just not true, for expressions in If "slow to develop" refers to my promise to write an RFC for pattern-matching expressions |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Totally understood -- did you see @Centril's clarifying comment? My understanding is that this RFC is meant to commit us to (1) some solution to this problem and (2) carving out a bit of space in the grammar so that the solution could be as discussed in the RFC. FWIW, I also have significant misgivings about solving this problem by essentially extending the special-cased treatment of @Centril I wonder if this should be made formally an "eRFC" to signify more clearly the issues above? |
This comment has been minimized.
This comment has been minimized.
|
@aturon |
This comment has been minimized.
This comment has been minimized.
|
These operators all have lower precedence than
Which means we also have the following ambiguity to consider: if let Range { start: _, end: false } = true..true && false {
println!("(current behavior)");
} else {
panic!();
} let result = loop {
if let Some(0u32) = break true && false {
panic!();
}
panic!();
};
assert_eq!(result, false);And then const F: fn() -> bool = || true;
if let Range { start: F, end } = F..|| false {
println!("(current behavior)");
assert!(!end());
} else {
panic!();
} let t = &&true;
if let Range { start: true, end } = t..&&false {
println!("(current behavior)");
assert_eq!(**end, false);
} else {
panic!();
}Please also ensure the parser accepts the following: if a || b {} // <-- no parenthesis, it is ok.
if a && b || c && d {}
// interpret as: `(a && b) || (c && d)`and rejects the following before if let Some(a) = b && c || d {}
// interpret as: `((let Some(a) = b) && c) || d` |
Centril
changed the title
RFC: if- and while-let-chains, take 2
eRFC: if- and while-let-chains, take 2
Jul 13, 2018
This comment has been minimized.
This comment has been minimized.
|
@aturon eRFC it is ;) @petrochenkov Yep, it's about making the proposed solution possible and recognizing that some solution (which could be I would personally be fine with experimenting with
I see; my impression was that it was more complicated from the discussions at #2260. This is the "bindings only live until the end of a full expression" rule?
Not at all :) It is referring to my perception of the support for
I thank you for it |
This comment has been minimized.
This comment has been minimized.
|
I can't say I'm a huge fan of the specific proposed syntax, but I'm inclined to take this step regardless, so that even if we don't end up going with this particular syntax we can detect potentially ambiguous uses of it. I'm honestly tempted to argue that either grouping should require parentheses. Minor nit: for consistency, can you please always put Also, in the example of how |
joshtriplett
reviewed
Jul 13, 2018
text/0000-if-let-chains.md Outdated
Centril
added some commits
Jul 13, 2018
Centril
self-assigned this
Jul 13, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Aug 24, 2018
|
The final comment period, with a disposition to merge, as per the review above, is now complete. |
rfcbot
added
finished-final-comment-period
and removed
final-comment-period
labels
Aug 24, 2018
Centril
merged commit d5e8e7b
into
rust-lang:master
Aug 24, 2018
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC has been merged! Tracking issues:
|
Centril
deleted the
Centril:rfc/let-chains-2
branch
Aug 24, 2018
Centril
referenced this pull request
Sep 15, 2018
Closed
Tracking issue for RFC 2086: Allow Irrefutable Patterns in if-let and while-let statements #44495
This was referenced Oct 7, 2018
jeremydavis519
referenced this pull request
Nov 3, 2018
Closed
Make bridge variables respect scopes better #1
Centril
added
A-syntax
A-control-flow
labels
Nov 23, 2018
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Dec 24, 2018
•
|
@Centril I somehow couldn't find in this RFC if the sudden precedence change of the
The phrasing here is not particularly clear (e.g. what is |
This comment has been minimized.
This comment has been minimized.
The precedence change is to bind
I agree; it wasn't my finest phrasing. These days I think it's better to think of this in terms of what happens to |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Dec 24, 2018
|
@Centril Thanks for the explanation! |
This comment has been minimized.
This comment has been minimized.
|
@H2CO3 Welcome :) |
rbartlensky
referenced this pull request
Jan 15, 2019
Merged
Global variables are now loaded from _ENV. #11
This comment has been minimized.
This comment has been minimized.
|
Argh, I didn’t see that RFC and I wish I did… I’m not a huge fan of the proposal, it seems too niche and introduces too much sugar (mixes two very different constructs — |
This comment has been minimized.
This comment has been minimized.
|
This is definitely not niche; in particular, this pattern arises frequently in the compiler (similar code like proc macros, clippy, anything that deals with AST-looking trees) and will pay for itself many times over. |
This comment has been minimized.
This comment has been minimized.
|
Yes, but
Which is definitely not part of the language (and shouldn’t, as introducing bindings in expressions seems completely out of question). So, yeah, maybe you’re introducing “conceptually simpler” sugar for very specific cases (again, I truly think it’s niche, and you put on a name on that niche: compilers and ASTs), but you’re also introducing confusion pretty much everywhere else where that “conceptually simpler” concept would feel applicable (while it’s not). |
This comment has been minimized.
This comment has been minimized.
|
I would also like to mention that |
This comment has been minimized.
This comment has been minimized.
|
Just yesterday I wanted this feature while changing the MPSC channels in |
This comment has been minimized.
This comment has been minimized.
|
In the code you showed, you could have used a guarded match. That’s already part of the language and it will do the job for most cases. Having lazy pattern-matching mixed with booleans makes me incomfortable. I’m not saying I’m against lazy pattern-matching (it’s actually handy). I’m just saying that using the |
Centril commentedJul 13, 2018
•
edited
Extends
if letandwhile let-expressions with chaining, allowing you to combine multiplelets andbool-typed conditions together naturally. After implementing this RFC, you'll be able to write, among other things:The main aim of this RFC is to decide that this is a problem worth solving as well as discussing a few available options. Most importantly, we want to make
let PAT = EXPR && ..a possible option for Rust 2018.To everyone who participated in RFC #2260 and to the survey participants.
To @scottmcm for collaborating with me on the original RFC.
To @aturon and @nikomatsakis for taking the time to discuss this with me.
To @SergioBenitez for checking the feasibility of this in Rust 2018.
To @oli-obk for providing me with the useful example in the summary ;)
To @joshtriplett for the consultation on the style.
To @kennytm for improving clarity on the Rust 2018 migration technical changes.