SE-0439: Allow trailing comma in comma-separated lists

I was weakly in support of SE-0084 by the time of the review (though I’d had the opposite opinion in the pitch phase); this time, my support is much stronger.

To be clear, I think trailing commas are ugly and slightly harm readability; if this proposal is accepted, I hope the Swift project style guide (and, if I had my druthers, most style guides and linting tools) will recommend that trailing commas only be used with specific justification. But there are situations where trailing comma use is justified. Off the top of my head, I can think of three:

  1. Temporarily while debugging or refactoring code to facilitate quick changes; hopefully these uses will be cleaned up before they are committed or published.

  2. When it is anticipated that the entries in the list will grow or change throughout the entire lifetime of the code base; think, for instance, of a list of version numbers or platform names which will inevitably accumulate new items as the project evolves.

  3. When code is being automatically generated at compile time and will rarely or never be read by human engineers.

That third use case is the main thing that’s changed since SE-0084 was rejected—the ecosystem is now generating a lot more code than it used to. In 2016, SwiftSyntax didn’t even exist yet. Now, eight years later, our core tools, like SwiftPM and the compiler, include built-in features like build tool plugins and macros to generate code that is rarely read and never maintained by humans. Correctly interleaving commas is a much bigger pain when you’re mechanically generating code than it is when you’re writing it by hand; that difficulty serves no technical purpose and rarely matters for readability since we’re talking about code that isn’t normally read. It makes sense to allow a more regular syntax here.

The fact that trailing commas are ugly doesn’t necessarily mean that the compiler should reject them. Their meaning is clear, they don't invite mistakes, and there are several situations we care about where they may be a good idea. I don’t think “it’s ugly” is enough to outweigh the reasons to permit them.

38 Likes