Semicolon? #1739
Replies: 7 comments 8 replies
-
I've used C++ and kotlin enough to notice how not needing semicolon improves readability |
Beta Was this translation helpful? Give feedback.
-
Semicolon waste of time |
Beta Was this translation helpful? Give feedback.
-
My recollection of the reasoning behind required semicolons is:
That being said, we do want it to be as easy as possible to determine if a Incidentally, automatic semicolons are a stronger restriction on the rest of the grammar. I would also like to note that the rules for automatic semicolon insertion end up complicated, and very different for different languages. We have a strong preference for simple rules, particularly at first. If something is shown to be a real problem in practice, we can always add complexity later. Comments saying "I like X" or "I don't like X" don't really contribute to the conversation. Much more helpful would be a reason for or against X that hasn't already been considered. |
Beta Was this translation helpful? Give feedback.
-
From a writability abd readability point of view, as long as having them doesn't impact the future design decisions to allow/not allow things like inlining and if statement then I'm personally fine either way. Trouble comes when it limits (or at least makes much less readable) future design decisions so as to have to get around having or not having them. Feels like something that could be decided much later on in the process once more formal specs have been decided for the rest of the language |
Beta Was this translation helpful? Give feedback.
-
Reasoning from the first principles, I can bring something to the table in favor of mandatory semicolons with references to the design principles First, we need to be specific about the alternatives to mandatory-semicolons, and I can think of two alternatives:
This approach is used by most popular imperative languages, i.e. JavaScript, Python, Go. These languages use Automatic Semicolon Insertion (ASI) or treat newlines at certain points as semicolons. One thing to understand about ASI is that it's not perfect substitute for semicolons. There exist edge cases where syntax is ambiguous to a human eye. For example
On the question of whether programmers who care about maintainability would format their code like that, probably not, but some formatters include a feature of splitting a long line into several lines if it exceedes a certain limit. It wouldn't be nice if your formatting tools messed up the way your code works. Also from time to time everyone writes long lines of code, making this behaviour someting to be afraid of violates these design principles:
Furthermore since optional semicolons make certain syntax ambiguous the following design principle is violated:
In the grand scheme of things ASI being unreliable is not much of an issue. Because, if you consider a tiny snippet of code above, there are other compiler warnings that could catch these spurious lines, for example "Having extra statements after Which might be violating:
This is another option for no-semicolons. If you know of any examples of imperative statement-based programming languages that do this feel free to reply. Due to lack of examples there isn't anything I know for certain about this approach so maybe it's the one worth discussing. I'm assuming that this would require treating newline as a token, and treating this token similarly to semicolon. The care still needs to be taken about where newlines terminates a statement and where it simply extends it to a new line. This also makes it more complicated to implement formatters that involve inserting newlines to keep lines short. Depending on how it's implemented the no-semicolon approach might be violating:
Which is an unfair thing to say, but unless there's a proposal for no-semicolons approach that would detail its semantics and reasoning, I don't think there's much to discuss. The ultimate tradeoff is whether convenience of not having to write semicolons when you don't have to is worth complicating tools by defining complex rules for statement parsing. For now I won't make any judgement of what's better, since I'm curious what the community has to say. |
Beta Was this translation helpful? Give feedback.
-
I think because carbon means not to utilize any modern IDE feature, it means to only use text based design, so ; should be very helpful. |
Beta Was this translation helpful? Give feedback.
-
I guess the reason Carbon chooses to add ";" as the statement terminator to be consistant with C++. |
Beta Was this translation helpful? Give feedback.
-
Why do we need semicolons?
224 votes ·
Beta Was this translation helpful? Give feedback.
All reactions