7 ms·
Design Note: Implicit Semicolons (2017)
- klodolph 6y agoI think of the different approaches available you can make a language workable & convenient with (1) explicit semicolons, like C/Java (2) implicit semicolons, like JS/Go/Haskell and (3) no semicolons like Python/Lua. JS’s specific implementation is definitely the odd one out, like the article says. One of the funny things about Go’s automatic semicolons is that you can easily trip up gofmt if you type sloppy code and expect gofmt to clean it up for you. For example, let’s say you type this: func foo() { if x < 3 { return 1 } return 2 } You think, “Oh, I missed the enter key when I was typing it but gofmt will fix it for me.” No, it will not. Syntax error.
- mdaniel 6y agono semicolons like Python That's not true, Python has semicolons and they're invaluable for composing `-c` command line scripts in a whitespace sensitive language
- bobbylarrybobby 6y agoGiven how capable our editors are today, it seems reasonable to both require semicolons at the end of statements and expect users to configure their editor/formatter to insert these semicolons for them wherever possible. The inability of the editor/formatter to automatically infer the locations of semicolons would be tantamount to a syntax error.
- Someone 6y agoIf your editor can do that, so could the compiler. So, why require them? The only reason I can think of would be readability, but I haven’t seen a language where that is a serious concern (scala gets close with its “there must be a blank line here” at times) I think “no semicolons” nowadays is the option to go for.
- ketralnis 6y ago> If your editor can do that, so could the compiler. So, why require them? So that there's a step in between where the human can check the result
- Rusky 6y agoEditors can get away with things compilers can't- they are inherently interactive. If your editor gets it wrong, you can just fix it. If your compiler gets it wrong, you might get an error message, or weird behavior. This is, for example, a major philosophy behind Rust's well-regarded error messages. The compiler rarely makes this kind of choice behind your back- instead, in reports an error message with a suggested fix-up for what it thinks you mean (and editor integration can apply those automatically). Then the program source remains unambiguous and clear, there are fewer ways to confuse readers, but you still get all the benefits of "if your editor can do that, so could the compiler."
- sammorrowdrums 6y agoCertainly in enjoy Prettier (in spite of its foibles) for JS because of this sort of stuff. It removes whole classes of lint errors and adds semi-colons and doesn't care how I format the code as I wrote it. Far too much thought and angst goes into things that should not need to be a concern. I used to feel more dubious about such tools, but after gofmt I tried Prettier and always use it now. It's just one more bit of cognitive load that is gone. Indeed, if it doesn't reformat my code as expected I know I've made an error, so it's even quicker for me at noticing some bugs than ESLint messages which are already pretty excellent these days.
- klodolph 6y agoI agree. I have autoformatting turned on for everything and I just don't care what my code looks like because it’s consistent and I can get used to it. I used to spend time configuring formatting settings to be exactly right, but nowadays I’m moving towards defaults. My only problem with gofmt is that since Go standardized on tabs, I’ve encountered problems with noisy commits because people rewrap comments, and the comments will wrap differently depending on the individual’s tab width setting.
- beeforpork 6y agoIn my opinion, a little redundancy is good to avoid rockets falling out of the sky, so I vote for a layout rule plus redundant semicolons. The semicolon is hardwired in many programmers hands anyway. I don't think it should be a design goal to get rid of it. Instead, the compiler's/interpreter's job is to help the programmer to avoid bugs, and with a little syntactic redundancy, the parser can already help out with a few syntax errors. I love gcc's -Werror=misleading-indentation.
- melling 6y agoI unhardwired myself using Swift. It is painful when I need to use them in another language. After coding for decades, I’m glad to have one less thing to type. If we want verifiable code, let’s move up a level of abstraction and build safer languages.
- regularfry 6y agoCounterpoint: I find a lack of semicolons entirely natural in ruby and python, and find it equally natural to insist on them in JS. I have no idea why this is.
- kjeetgill 6y agoY'know I was just musing over this exact feeling myself. I think some languages are more suited to it than others just by their "look". It's just a matter of trade dress — semicolons just go with curly braces. I find their absence in go jarring too.
- bigyikes 6y agoWhat really keeps me up at night is wondering about the semicolon after a class definition: class Foo { ... };
- regularfry 6y agoThis doesn't get one. It's confusing, but explicit in the ecmascript spec. That form of class definition is called a ClassDeclaration, and they don't take semicolons. I haven't checked, but I think what you've typed here is valid because the parser sees a zero-length expression after the '}'. The reason it's confusing is because if you modify it very slightly, then the semicolon becomes required: let Bar = class Foo { ... }; In that context, precisely the same sequence of characters as you had before is now called a ClassExpression, and the semicolon isn't attached to the class, but instead to the preceding let. I do find semicolons useful as an indicator at the end of the class expression that there's a let or a const somewhere above, but it's far simpler to just not do that and pretend that language facility doesn't exist unless you really, really need it.
- secondcoming 6y agoBASIC had the 'End' keyword instead of a delimiter.
- toolslive 6y agoSemantics were different. End ended the program. https://www.c64-wiki.com/wiki/END https://www.c64-wiki.com/wiki/END Pascal however used BEGIN and END to denote the beginning and the end of blocks. Maybe your memory got garbled.
- Forge36 6y agoVB6 used "End X", such as "End Function" and "End If"
- gnufx 6y agoFor what it's worth, Fortran goes further with (optional) "end function X".
- toolslive 6y agoyou don't semicolons, but they can be handy. One way of looking at them is as an infix operator (a monad if you want). Another remark is that Python parsing can be hard as a single newline can actually end multiple blocks. Lexers are not designed to have a single char denote multiple tokens.
- gnufx 6y agoCan't semicolons essentially be either infix or postfix (terminators), depending on the language? Parsing the offside rule -- I know that's not actually Python -- can be done straightforwardly with a parsing combinator transformer (originally due to Hutton?) if tokens are associated with layout information.
- draw_down 6y agoFWIW, eliding semicolons in JavaScript is perfectly doable. While much is made of the rules for automatic semicolon insertion, in practice you don't need to think about them. You can just code without ending your lines in `;`. I promise it will be ok. As an example, standardjs (people used to get really mad about the name): https://standardjs.com https://standardjs.com
- Smaug123 6y agoI find Mathematica very neat in the semicolons department (possibly because it's the only Lisp I know, and because all Lisps are neat in this way). In Mathematica, a semicolon is nothing more nor less than an infix syntactic sugar for `CompoundExpression`, and `CompoundExpression` has the attribute `HoldAll` to prevent eager evaluation of the syntax tree. You could roll your own very easily, complete with its own infix operator. You can also override the default evaluation strategy of `CompoundExpression` if you `Unprotect` it first, or you can define upvalues so that your own things behave differently when they appear inside `CompoundExpression`s. It's all very lovely.
- kmill 6y agoI've never thought about adding upvalues to CompoundExpression -- what kinds of applications have you used this for?
- Smaug123 6y agoLiterally none, ever. But you have the option!
- Smaug123 6y agoIn fact, here's a silly idea, executed with two kernels: SetAttributes[Parallel, HoldAll]; CompoundExpression[x__Parallel] ^:= ParallelCombine[Identity @@@ # &, {x}] In[28]:= (Parallel[Pause[3]; 3]; Parallel[Pause[3]; 4]; Parallel[Pause[3]; 5]) // AbsoluteTiming Out[28]= {6.00864, {3, 4, 5}}
- chubot 6y agoI just solved this problem in Oil. In our two main influences, shell and Python, newlines are significant. However Oil uses {} for blocks, and it also has dicts. Originally I thought we should have something like %{} for dicts to give the lexer and easier job when deciding when newlines are significant. Oil uses Python's rules of newlines being ignored between () and [], and it would make sense to ignore them inside {} but not %{}. But I found I could just makes newlines significant within {} and then put them in the grammar for dicts, as well as for blocks. It seems to work fine. http://www.oilshell.org/blog/2020/10/big-changes.html#dicts-are-not http://www.oilshell.org/blog/2020/10/big-changes.html#dicts-... I am open to feedback if anyone wants to kick the tires :)
- kzrdude 6y ago"implicit" or "automatic" semicolons should not exist. If there are conceptual semicolons, they should be visible. A language that is semicolon free should not have a rule for invisible/phantom/imagined semicolons, that is not the way to free the programmer from thinking about it.
- userbinator 6y agoIt's always seemed odd to me that a language with significant whitespace like Python became so popular, because human languages naturally have separators like semicolons and periods between sentences; otherwise, it all looks like one big run-on sentence. I admittedly work a lot in C and C-derived languages, and while reading I mostly disregard whitespace (as the compiler also does), so the latter is what code in other languages without statement separators/terminators looks like.
- ryan-duve 6y agoI see the point about dots, commas and colons being helpful. I just used them in the previous sentence for readability! On the other hand, I worked exclusively in C-like languages until about five years ago (I almost exclusively use Python now) and I made the opposite observation. In English articles, indented lists and paragraph breaks really pop, helping me understand the structure of what I'm reading at a first glance. When I realized Python's whitespace gives me the same thing, I find it hard to go back to curly braces defining blocks unless there's an additional convention of whitespace-indentation on top.
- qppo 6y agoMy pet theory is that a lack of non-whitespace separators heavily discourages clever one-liners or compact sequences of operations and forces the programmer to write more readable code.
- labawi 6y agoPython actually has ';', and I think you can write everything in one line if you really want to, e.g. in an inline script python3 -c 'import math; tau=math.pi*2; r=1; u=r*tau; s=r*r*tau/2; v=r*r*r*tau*2/3; print(u,s,v)'
- marvy 6y agoIn this simple script you can, but eventually you need an loop or something and you realize you're kind of stuck: before_loop; while something: step1; step2; after_loop; Note that after_loop is still inside the loop. Also you can't have multiple colons on the same line, so this doesn't work either: if test: do_this; else: do_that That gives a syntax error
- filoeleven 6y agoS-expressions gracefully disambiguate all three examples in the note, with nary a semicolon nor a comma in sight. first -second will either be (- first second) or (though this is a bit silly) (do first (- second)) and there is never any danger of mistaking one for the other.