[swift-evolution] Allowing trailing commas in argument lists
grp at fb.com
Wed Mar 9 13:34:49 CST 2016
Thanks for the feedback everyone! A few more notes to add to the proposal:
- Rust also supports trailing commas in argument lists and tuples.
- Alternative proposals suggested by Haravikk:
1. Support trailing commas only in multi-line argument and tuple declarations. This is an interesting approach, and would codify good practice in the language. It might make the most sense for a lint-style tool. On the other hand, in Swift, the choice of whitespace character is usually not significant. Putting this rule in the language would change that for only a small benefit.
2. Allowing trailing commas except when it could cause ambiguity; for example, with type inference, overloading, or default parameters. This seems like the most complete way to prevent mistakes, but it also seems like it might be difficult to implement if the allowed syntax changed based on the type checker. Would have to defer to people with more experience in the Swift compiler if this is feasible.
Patrick and Ted —
Do you also see the same issue with incomplete thoughts when writing arrays or dictionaries, where Swift does currently support trailing commas? I think that case would be more likely than in argument lists: an incomplete argument list would fail to compile in most cases from an incorrect number of parameters, but an incomplete dictionary or array would necessarily still compile.
It could be a reasonable proposal to disallow trailing commas in all places, albeit one I would disagree with.
> On Mar 9, 2016, at 10:55, Patrick Gili <gili.patrick.r at gili-labs.com> wrote:
> I have to agree with Ted. When I leave a "trailing comma", it's almost always because I constructing a call and didn't finish, for whatever reason. Thankfully, the compiler is nice enough to tell me when I committed this error.
>> On Mar 9, 2016, at 9:44 AM, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org> wrote:
>> Hi Paul
>> I don’t think that is a good idea because:
>> as in most human languages writing,
>> the comma tells us,
>> that more is following,
>> .. oops. hey, no more text?
>> So, to me your examples are incomplete statements,
>> leading to mistakes when editing, me assuming that I
>> didn’t finish typing the statement, when I started
>> editing the source say, three months later.
>> or, doing maintenance and having to fast-read through
>> many old source files.
>> Is it really so hard to press the DEL key to remove
>> the trailing comma?
>> TedvG ,
>> (sorry no more text here :0)
>>> Right now, Swift argument lists are not permitted to contain trailing commas. To make multi-line calls easier, we propose allowing trailing commas in argument (and tuple) syntax:
>>> let person = Person(
>>> id: json['id'],
>>> name: json['name'],
>>> picture: Im2age(picture),
>>> friends: friends,
>> swift-evolution mailing list
>> swift-evolution at swift.org
More information about the swift-evolution