Nontrailing separators do not spark joy
15 points by fanf
15 points by fanf
The JSON grammar specifies that a comma can separate two members of an object but not postcede ("trail") a member. I think this was a design mistake.
There are lots of things you can call it, but "a design mistake" is not one of them, because it was not a choice: JSON was created circa 2000~2001 as a subset of ECMAScript 3, the informational RFC 4627 was written in 2006. That it was a subset of javascript and worked out of the box in browsers (via eval) was both its very purpose and critical to its success, native JSON APIs were only added to browsers in 2009.
Trailing commas were specified in ES5, published December 2009.
JSON-with-trailing-commas could not have existed, because it would not have been fit for purpose.
I 100% agree. Any new language that doesn't have trailing separators get's a small negative mark from me. Not enough to make me hate the language's syntax but it's a papercut.
Even in function calls? foo(1,2,3,4,)? bar(,1,2,3,4)? If foo() allows a variable number of parameters, is the last one nil? Is the first parameter of bar() nil?
I've never seen a language with the behavior you specified - foo(1,2,3,4,) is exactly the same as foo(1,2,3,4) and bar(,1,2,3,4) is invalid (as well as reduced example bar(,))
There were flavors of BASIC in the 80s that allowed such syntax. The bar() example follows the "partial bullet point" stye of the article.
That is definitely one of my most-encountered pain points with Prolog. When I’m working on a predicate, I often end it with ,true. so I can easily re-order and comment-out the lines above with worrying about juggling that final full-stop.
Yep. Similarly, in SQL, our team writes WHERE clauses as where true / and ... / and ... (the slashes are line breaks) so that any clause can easily be edited without special treatment.
Zig allows trailing commas and they can be used to drive the (otherwise non-configurable) formatter.
.{1, 2, 3,} becomes:
.{
1,
2,
3,
}
And taking the trailing comma away from a vertically-formatted literal requests horizontal alignment (.{ 1, 2, 3 }).
Also works with:
struct { a: u32, b: u32, }
fn foo(a: u32, b: u32,) void {}
foo(1, 2,);
In all of these cases you can use a trailing comma to drive auto-formatting.
I like this feature so much that I added it to my HTML language server / autoformatter:
Before:
<div foo="bar" style="very-long-string" >Foo</div>
After:
<div foo="bar"
style="very-long-string"
>Foo</div>