5 ms·
Braces, maybe. But I've always felt that semicolons at the end of lines were just noise. I can tell it's the end of the line because it's the end of the line. T
by gary_0 2y ago
Braces, maybe. But I've always felt that semicolons at the end of lines were just noise. I can tell it's the end of the line because it's the end of the line. The few cases of ambiguity that happen can be solved by common sense (in the programmer or in the parser).
- ilyagr 2y agoThis is what golang does. https://go.dev/doc/effective_go#semicolons https://go.dev/doc/effective_go#semicolons
- xpl 2y agoAlso, JavaScript.
- gary_0 2y agoAnd Kotlin and Swift.
- xigoi 2y agoExcept JavaScript does it wrong.
- trealira 2y agoWhat makes Go's semicolon insertion algorithm different from JavaScript's? They seem similar to me. Isn't it just that the Go compiler requires very strict formatting, which saves you from getting bitten by errors due to unexpected semicolons?
- xigoi 2y agoThe following (contrived) JavaScript code does not work: let x = 5 (function() { console.log(x) })() The equivalent Go code works: package main import "fmt" func main() { x := 5 (func() { fmt.Printf("%d", x) })() }
- trealira 2y agoThanks, that's a good case. Actually, the JavaScript semicolon insertion algorithm [0] seems more complex than I had remembered. I had thought of them as variations on the semicolon insertion in BCPL, but it seems like that's an oversimplification. For reference, these are the BCPL rules: The canonical symbol SEMICOLON is inserted between pairs of items if they appeared on different lines and if the first was from the set of items which may end a command or definition, namely: BREAK RETURN FINISH REPEAT SKET RKET SECTKET NAME STRINGCONST NUMBER TRUE FALSE and the second is from the set of items which may start: a command, namely: TEST FOR IF UNLESS UNTIL WHILE GOTO RESULTIS CASE DEFAULT BREAK RETURN FINISH SECTBRA RBRA VALOF LV RV NAME ... ( ) [ ] § § are used to denote RBRA RKET SBRA SKET SECTBRA and SECTKET. [0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- kazinator 2y agoBourne and POSIX shell: for x in *.txt; do echo $x; done for x in *.txt do echo $x done
- trealira 2y agoHaskell also inserts semicolons, but it has a different rule, which I think is a bit interesting. If the current line starts on the same line that the previous expression started on, then a semicolon is inserted at the beginning of the current line. There are also some messy rules about opening and closing braces being inserted. So, this: do expr1 expr2 do expr3 expr4 Ends up being parsed as though it were written like this: do { expr1 ; expr2 do { expr3 ; expr4 } } If the next line is indented, it's just taken as a continuation of the previous line's expression, so nothing needs to happen. [0]: https://amelia.how/posts/parsing-layout.html https://amelia.how/posts/parsing-layout.html
- nomel 2y agoSemicolons are already supported in Python, for the cases where it might be needed (one liners on the command line), just not required. import sys; print(sys.executable) x = 5; # this is fine
- darby_nine 2y ago> But I've always felt that semicolons at the end of lines were just noise. I felt this way until I engaged in code with very horizontal coding styles. Semicolons make it extremely easy to visually break up sequential statements from expressions consuming multiple lines.
- gary_0 2y ago> code with very horizontal coding styles I'm not sure what you mean. Could you give an example?
- darby_nine 2y agoLiterally just code with long lines. You run into this particularly with monadic stuff like LINQ queries.
- gary_0 2y agoI grepped up some old LINQ code I wrote (sanitized as it's not open source): var list = obj.FindDataNear(a, b...).Select(i => obj.GetPosition(i)) .Where(...long lambda...) .MoreLINQ() .ToList(); So with the style I used, the semicolon is superfluous due to indentation. Are there styles where the semicolon is useful?
- darby_nine 2y ago> So with the style I used, the semicolon is superfluous due to indentation. Eh, indentation is also conflated with block delimitation. The semicolon is still useful to more strongly indicate statement delimitation at a glance. Yes, it's redundant, but so are many signifiers around us. The pity is that readability aspects aren't semantically relevant but are stored as if they are.
- voidUpdate 2y agoThe end of the line isn't always the end of the line, such is if there is a chain of methods and you are limited to 80 columns. Then you can just wrap on a . , and tell the language where you actually want the line to end