Skip to content

Have mandatory braces, no parenthesis in the if/for/while syntax #1462

@laurentlb

Description

@laurentlb

The if/else proposal mentions two alternatives at the end: "No parentheses" and "Require braces". The conclusion was:

The benefits of this are debatable, and it should be examined by an advocate in a focused proposal. For now, we should match C++'s decision.

I would like to discuss the topic a bit more.

  • In C++, braces after a if are optional. In practice, many style guides require the use of braces, e.g. https://google.github.io/styleguide/cppguide.html#Conditionals
  • Giving the choice of having braces doesn't seem useful, if style guides are likely to discourage it. It also conflicts with the One Way Principle.
  • Making braces mandatory allows us to remove the parenthesis around the expression:
if x {
  do_parse = false;
}
  • Parenthesis are already used in lots of different places in the grammar: function calls, precedence, generics... Removing parenthesis here can reduce the overloading.
  • This idea has been successfully used in other languages (e.g. Go), and probably won't disrupt people familiar with C++ (if they put the parenthesis after a if, they would just be useless).
  • Removing the parenthesis would be more consistent with the Conditional expressions syntax (if cond then x else y).

For consistency, the syntax of while and for would need to be updated as well:

while x { ... }

for var i : mylist { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    leads questionA question for the leads team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions