r/ProgrammingLanguages • u/bjzaba Pikelet, Fathom • Jun 11 '26
Nontrailing separators do not spark joy
https://buttondown.com/hillelwayne/archive/nontrailing-separators-do-not-spark-joy/
57
Upvotes
24
u/rhet0rica Jun 11 '26
Fun fact: the first language to use
;as a statement terminator was ALGOL 58—but it wasn't a terminator, it was a separator, much like the,in JSON. A trailing semicolon in a block might be an error to some parsers! So, there is very real precedent for permitting trailing commas.31
u/igors84 Jun 11 '26
I really like how Zig lang did this. It allows trailing commas and they actually use it as a formatting hint. I have setup my editor to do format on save and if I have some array literal like
.{ 5, 6, 7}it will be in one line and if I now want to add one more element but have it split into multiple lines I just add, 8,and that trailing comma will tell the formatter to split elements into new lines and so when I press Save that code will automatically become:This works in many places and is really convenient way to format your code.
9
u/Red-Krow Jun 11 '26 edited Jun 11 '26
I like the way F# does it. Newlines are a valid list separator, so you don't need trailing commas because you don't need commas:
It's actually the same distinction for statements:
You have to use separators when you're writing items in the same line, but, in that case, having a trailing separator doesn't make diffing easier.
(Note: The choice of the semicolon for lists is weird but justified. In F# tuples don't require parenthesis, so using commas would make the expression [1,2] ambiguous)
8
u/Tasty_Replacement_29 Bau Jun 11 '26
This is a surprisingly complex problem. The article talks about JSON data, but I think the following problems are related:
Trailing commas solve a problem that only exists because commas are mandatory in the first place. The most elegant option is not having a comma:
Even here there are multiple options: If there is a newline, is the comma even allowed? And what if there is no newline, is the comma needed? For the case above, probably yes, because without comma it would be hard to read for a human.
Lisp does not have commas for simple values (e.g. a list of numbers), and command line don't have commas between arguments:
So, I think the best rule would be:
At least, that is what my programming language is doing. So you can write eg.
With this rule, I do not see much need for string interpolation: you only save one character in this case:
If expressions are complex, then a comma or
()are needed:My language does not have an array initialization syntax, but instead uses a "array" function with varargs. Also here, commas are optional. To me, the second version is easier to scan:
The trade-off is that the language must define exactly when whitespace acts as a separator. But if those rules are simple, I think whitespace and newlines often produce cleaner, more succinct code than mandatory separators and trailing commas.
10
u/jonathancast globalscript Jun 11 '26
Yeah, no, just treat commas as terminators, please.
Or mandate 'bullet points' if you really want to, but all code in a single language should be consistent.
(Although I force punctuation (,, ;, and kind of.) and right associative operators to trail, and left associative operators to be bullet points, so maybe I'm violating my own advice?)
3
u/MichalMarsalek Jun 11 '26
In my language I made the design decision pretty early that commas, semicolons a s newlines are all always interchangeable and extra separators are ignored (so you can have a comma followed by newline or trailing comma/newline).
2
u/Ratstail91 The Toy Programming Language Jun 11 '26
Haskell does what
I should learn more obscure languages.
2
u/CharlemagneAdelaar Jun 11 '26
MATLAB lets you throw a semicolon, comma, or anything else between lines in a multi-line declaration for, say, a cell array or something. Really helpful
2
u/awoocent Jun 11 '26
This post did remind me I should probably support trailing separators in my parser, but honestly every time this comes up it just strikes me as the biggest bikeshed ever. Like does anyone actually spent any significant amount of time or effort deleting a trailing comma? It's never really bothered me writing code, and it strikes me as probably worse for reading code - whenever I see a comma I feel like it implies "something comes after", so it's a minor stumble to read that and then see there's nothing actually after it and rewind. Given code is often read many more times than it is written, I feel like a little extra effort polishing stuff up is only reasonable.
1
u/alkalisun Jun 11 '26
Love the article. I'm thoroughly terrified of Alloy now, thanks for that :)
1
u/FirmSupermarket6933 Jun 12 '26
There is also JSON5 format in which second code snippet is valid
1
u/DonnPT Jun 12 '26
For me it's a serious beef with Ocaml.
Excuse me if that isn't valid code, I don't have Ocaml on my computer here any more, but Ocaml has statement blocks - not being a strictly functional programming language - and they're semicolon separated. With no required enclosing punctuation, so effectively the same as separator joined, creating an ambiguity that's resolved by associativity or something. So I can indent the above so it looks like the semicolon is separating the match statement from the final print_string, but it's really separating two print_string statements in the [] tail of the match expression.
1
u/Ishax Strata Jun 14 '26
If you're willing to introduce spacing based elements, you could require trailing separators in line-final positions.