-
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-else could allow omitting braces on the else #1616
Comments
I feel this is a loss of symmetry for very little gain. The second drawback shows that this opens up Rust to The "more sophisticated" alternative (allow any braced block in the |
I'd be in support of the "more sophisticated" alternative - I've definitely wanted That said, since I only really want |
To try --- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3258,6 +3258,8 @@ impl<'a> Parser<'a> {
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
if self.eat_keyword(keywords::If) {
return self.parse_if_expr(None);
+ } else if self.eat_keyword(keywords::Match) {
+ return self.parse_match_expr(None);
} else {
let blk = self.parse_block()?;
return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), None)); |
Me too, but I'm not sure I like the idea of I think it has something to do with how many expressions you need to process before hitting the |
I don't think general omission is a good idea. Being able to write: if cond {
foo();
}
else bar;
baz; is begging for bugs to happen. Way too ambiguous. I like the idea of being able to write this though: if cond {
foo();
}
else match x {
pat => bar(),
_ => {},
} I've found myself trying to write that a few times before. Rust already has a lot of brackets and visual noise, and match has a nasty tendency to rightwards drift. As long as it's unambiguous, I think it would improve readability. Rust being a functional language, it would make sense that |
+1 I wouldn't mind a |
Making |
I'd hope that |
👍 for If this syntactic sugar is concisely elaborated in its own RFC, I'll happily 👍 it. |
@burdges no it would not, that wouldn't make any sense. I proposed that brackets directly surrounding a match statement can be eliminated because if cond1 {
...
}
else if cond2 match x {
...
}
else match y {
...
} It's just a bit of syntactic sugar to reduce the noise of extra brackets and remove one level of indentations. |
By the way, this kind of stuff already exists in a small form and pretty much every programmer writing in a C-style language has already written it. The Writing if foo() {
do_this()
} else if bar() {
do_that()
} else if baz() {
do_this_other_thing();
} is the same as writing if foo() {
do_this()
} else {
if bar() {
do_that()
} else {
if baz() {
do_this_other_thing();
}
}
} Of course we don't have a |
I am vehemently opposed to this addition for the drawback that you describe, if foo() {
...
} else
bar();
baz(); as others have pointed out this is just begging for bugs. The omission of braces do cause bugs in everything from student submissions to production-code. I'd prefer if we didn't invite it into the Rust language. |
I wouldn't be opposed for specifically |
An RFC that proposed this but ended up closed: #1712 (comment) |
Thinking I'm gonna close this too to reduce clutter :) (if anyone disagrees, feel free to reopen) |
Background
As we all know, Rust doesn't require parentheses around the condition of an
if
, but does require braces around the bodies ofif
andelse
.This is good because it avoids the ambiguity and potential for mistakes around nested
if
s andelse
s that exists in other C-family languages.The problematic example is this:
In current Rust, that example must look like this:
Or like this:
In either case, the meaning is now explicit and unambiguous.
The idea
But as far as I can tell, only the first pair of braces are in fact required to accomplish this.
If we only put braces on the
if
:It's still just as explicit.
Advantages
The potential benefits of relaxing this would be:
We could avoid a proliferation of braces and indentation when the
else
block consists entirely of another construct delimited by braces, such as amatch
. For example, this:Could instead be this:
As a special case of the above, we currently allow writing
if
...else if
...else
chains "naturally", as opposed to requiringelse { if {
... by simply giving special treatment toif else
. This would now "fall out" of the general rule, and would no longer require special casing.Drawbacks
It's a bit less symmetric.
There'd still be the hazard that you could accidentally write:
and imagine that the
baz()
is in theelse
, when it's actually not (a lagoto fail
).A slightly more sophisticated rule that avoids this hazard would be that you may omit the braces around the body of an
else
if the body is a single expression which itself requires braces. So you could writeelse match { ... }
,else for { ... }
,else loop { ... }
,else if { ... }
, and so on, but notelse bar()
orelse quux()
.Evaluation
In my estimation, in terms of theoretical elegance getting to remove the
if else
special case would roughly cancel out with breaking theif ... { ... } else { ... }
symmetry, leaving the practical benefit of fewer braces and less rightward drift, which is a net win.The text was updated successfully, but these errors were encountered: