4 ms·
I came across this formatting pattern in Haskell, but I still prefer trailing commas for one reason: I can trivially apply line-wise operations (e.g. sort or al
by majewsky 10y ago
I came across this formatting pattern in Haskell, but I still prefer trailing commas for one reason: I can trivially apply line-wise operations (e.g. sort or align) on the key-value pairs without breaking the syntax. When I sort your first snippet line-by-line, it becomes
, "age": 229
, "appearance": "Tree-like. It's a tree."
, "sex": "monoecious species"
{ "time": "3 minutes past four"
}
and the syntax is broken. With trailing commas, the syntax always stays valid:
{
"age": 229,
"appearance": "Tree-like. It's a tree.",
"sex": "monoecious species",
"time": "3 minutes past four",
}
- tome 10y agoWith prefix commas it also always stays valid { , "age": 229 , "appearance": "Tree-like. It's a tree." , "sex": "monoecious species" , "time": "3 minutes past four" }
- saurik 10y agoYou are assuming that trailing commas are ignored as valid, which is true of JavaScript but not of JSON or of C (the other example).
- majewsky 10y agoYes, I'm aware of this being an error in JSON (which is precisely the point of the discussion). I wasn't aware that it's a problem in C. Maybe gcc is more lenient here.
- tptacek 10y agoTrailing commas are allowed in C!
- mzs 10y agoC89 v. C99 difference IIRC edit: I was wrong: http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#initializer http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#initialize...
- saurik 10y agoNo: tptacek did not bother paying attention to the context; as far as I know "uint8_t * data, * buffer, ;" (spaces added after * to avoid italics) would never be valid.
- tptacek 10y agoHow is that the context? In the sense of the intersection between Javascript and JSON, trailing commas have pretty much always worked in C, which is something all of us who write code that generates C rely on, like a lot.
- mzs 10y agoI googled and trailing comma was allowed since C89, I was wrong. For code that generates code I always did something like this cause of that mistake: { a, b , c ... , z };
- saurik 10y agoThe contextual example from C was "uint8_t * data, * buffer;" (spaces added to avoid italics), and no: a trailing comma is absolutely not allowed.
- tptacek 10y agoReplied here, to the first time I saw you write this: https://news.ycombinator.com/item?id=12368022 https://news.ycombinator.com/item?id=12368022