Allowing trailing commas in argument lists

I didn’t even know arrays supported trailing commas until I read this thread, so it seems allowing it everywhere will give an additional flexibility to a subset of developers but not “pollute” things for the rest of us (if I can be so bold as to assume the majority prefer the cleaner approach.)

Yeah, I only discovered it by accident maybe two months ago — for >1.5 years of using Swit, I didn’t know it was allowed. Many people have said that — which, I think, is good evidence that, even if not widely used, there’s no harm in allowing it.

Although I’d be saddened by the removal of trailing commas in arrays, I agree it should be consistent.

— Radek

···

On 10 Mar 2016, at 20:37, David James <davidbjames1@gmail.com> wrote:

Seems we should either add trailing commas for ALL arbitrarily ordered lists of things *OR* remove any allowed trailing commas that exist at present (like arrays). To be consistent.

I didn’t even know arrays supported trailing commas until I read this thread, so it seems allowing it everywhere will give an additional flexibility to a subset of developers but not “pollute” things for the rest of us (if I can be so bold as to assume the majority prefer the cleaner approach.)

David

On Mar 10, 2016, at 4:35 PM, Nisse Bergman via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I’m not upset at all. I just think the benefits does not warrant a change in the language :)

Nisse

On 10 Mar 2016, at 17:28, Radosław Pietruszewski <radexpl@gmail.com <mailto:radexpl@gmail.com>> wrote:

Trailing comma is not nonsensical, there is a specific purpose in allowing it (unlike your examples), and there’s precedent in Swift already.

Why does this proposal make you so upset?

— Radek

On 10 Mar 2016, at 16:30, Nisse Bergman <nisse@potmo.com <mailto:nisse@potmo.com>> wrote:

Should it also be ok to write other nonsensical characters in the code?
I know this is a bit silly examples but should this be OK?
if a>b hello#€€ {c = b}else ((wow)) {c=a;swap(a,b);arrgh(arrghument) asd asd asd asd}}}}}}
Should we allow extra curly braces where the compiler can remove unnecessary characters?
struct Cool {{{{{
}}}}}}}}

I don’t see any reason to allow two syntaxes just because you get two lines changed in a diff when you just added one.
Of course you should be able to format your code however you want. Space indent or tabs? I don’t care but this is not formatting, this is syntax.
I actually think that it is good for the community that the formatting stay kind of the same.

If you want trailing commas then remove the syntax for non-trailing commas.

Nisse

On 10 Mar 2016, at 13:34, ted van gaalen via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Oh no, hurry!! switch of your screens!!
Hide under your desks!!
The SourcePolice-- has arrived!!! :o)

Imho people should be free to format and style their source in any way
they like -apart from company and team enforced standards when
working for or with them, of course.
For example, You might have noticed on this forum, e.g. that I always
put brackets on a new line, because I find it much easier to read.

(not doing so, I personally consider this as a traditional bad habit,
taken over from C and C++, being useful only in long gone days of limited
screen estate, when printing out sources was often the only way to have a
good impression. Or in digital medieval times, It saved a lot of weight (literaly)
when I still had to edit with punched cards as source carrier, as each card
could only contain 1 line.)

Xcode respects largely personal preferences and e.g. in Eclipse, Netbeans
one can set one's own formatting preferences. However, for instance, the
Dart-language-people, in al their unlimited wisdom, found it necessary to
enforce a formatting dictatorship. You can't customize source formatting.
What they enforce is in their vision the only perfect way. Silly.

So, it you want to write code like ( fasten seatbelts, please)

   if a>b {c = b}else{c=a;swap(a,b);arrgh(arrghument)} // etc.

That's ok with me.. as long as I don't have to maintain your code.
The only (formatting and other) standards one should, or rather, must
conform to are those necessary when working in a team, or e.g.
when contributing source entities to Swift open source.

And where is the end? you could even take this 1984 style dictatorship
much further, e.g. by not allowing identifier names to be < 3 characters etc.
(or removing certain unstylish source elements, but that's another matter, is it not?)

I'd say, it is rather a matter of commonSense++.

I've worked long ago in a Cobol team where it was not allowed to
start an if block with a "not" like so (in Swift):
  if ( ! someFunc(value)
  {
       spanishInquisition()
  }
we then had to write it like so:
  if someFunc(value)
  {
  }
  else
  {
       spanishInquisition()
  }
Because that was the company's way of doing things,
stemming from the endless wisdom of the local gods,

?
(sorry CommaConfusion (wow, that's a really cool name for a band!))

people that worked there for twenty years or so,
without noticing that there is a world outside their fences...

but perhaps it also has to do with the fact that,
at least for building my own apps++, I've metamorphosed
into being an Indie Developer, and some aspects of
that are very cool, like not loosing 50% of my time
(although having plenty of it) for team coordination.
The luxury of doing things, at least in this domain,
the way I prefer.

TedvG.

On 10 Mar 2016, at 09:09, Radek Pietruszewski <radexpl@gmail.com <mailto:radexpl@gmail.com>> wrote:

This is a linter's job.

Sent from my iPad

On 10 Mar 2016, at 05:28, Paul Ossenbruggen via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Most people are good about it but there is always one or two who take advantage of something like this, and if you allow it they will. I think it would drive me crazy to see this:

let person = Person( id: json['id’], name: json['name’], picture: Im2age(picture), friends: friends,)

This would drive the anally retentive people crazy! I mean Swift was great that it got rid of the braces here:

if x == y {
} else {
}

so no more running the parenthesis right up against the if. But someone I know does this now:

if x == y{
}else{
}

Arrrgh!

Can we force the space before and after the paren? Without requiring uncrustify? :-)

On Mar 9, 2016, at 12:47 PM, Patrick Gili via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Mar 9, 2016, at 2:34 PM, Grant Paul <grp@fb.com <mailto:grp@fb.com>> wrote:

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.

Nisse —

Echoing what Radek said, the JavaScript “comma first” style comes directly from a *lack* of support for trailing commas (in Internet Explorer specifically). That style appears to have been first suggested here: A better coding convention for lists and object literals in JavaScript · GitHub — part of the rationale was "a final trailing comma will cause errors in MSIE”.

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.

Absolutely.

I work on a geographically diverse team, which demands an interrupt-driven environment. Dealing with phone calls, emails, and IMs must take priority to maintain communication between team members. Sometimes those interrupts deal with code. Thus, if I'm coding a call site or an array/dictionary initialization site when an interrupt comes in, it is likely that I don't switch back to the context, leaving some incomplete code. I'm sure if I put my head to it, I could think of other places allowing trailing commands are going to hurt me more than help me.

It could be a reasonable proposal to disallow trailing commas in all places, albeit one I would disagree with.

Grant

On Mar 9, 2016, at 10:55, Patrick Gili <gili.patrick.r@gili-labs.com <mailto:gili.patrick.r@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.

Cheers,
-Patrick

On Mar 9, 2016, at 9:44 AM, Ted F.A. van Gaalen via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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?

Greetings,
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@swift.org <mailto:swift-evolution@swift.org>
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Devolution&d=CwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=TiMad4REPS_rpOXRZiQytQ&m=5hE4Yb04I38N2u3hF1YNWi2rnd6td7WW3_sIMgH1UHU&s=QncXBAO3MI-M5GEJ-wFM9qPqCReaGIzogre9d5EubAE&e= <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Devolution&d=CwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=TiMad4REPS_rpOXRZiQytQ&m=5hE4Yb04I38N2u3hF1YNWi2rnd6td7WW3_sIMgH1UHU&s=QncXBAO3MI-M5GEJ-wFM9qPqCReaGIzogre9d5EubAE&e=&gt;

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

David James

H Erica.

In working with many programming languages since
ca. 1980, there was not even one language offering this,
and I am an experienced programmer.

What do you mean with “reordering” in this context?
   1. only with manually editing ?
or
   2. With some form of source automation ?
or
   3. reason(s) unknown to me?

Btw. please note that Swift should also be accessible and usable for less experienced programmers.

Sorry, Erica, another topic, but if in what you wrote here you'd replace “trailing commas” with “for ; ;” you would have described exactly
my opinion about the for ;; statement. a bit contradicting, i think.

For those of us who prefer trailing commas, it will be both a convenience and an asset. For those against, there's no harm
done. I find it unlikely that trailing commas will naturally lead to any unsafe code or produce a net negative effect on the
language or the code-base it supports.

Please note, not meant unfriendly.

Kind regards
Ted

···

On 10.03.2016, at 21:27, Erica Sadun <erica@ericasadun.com> wrote:

On 10 Mar 2016, at 17:28, Radosław Pietruszewski <radexpl@gmail.com> wrote:

Trailing comma is not nonsensical, there is a specific purpose in allowing it (unlike your examples), and there’s precedent in Swift already.

Trailing commas enable easier re-ordering of arguments, simplify growing or compacting argument lists, and do no harm.

Their use in arrays and dictionaries are conventional enough that experienced programmers will not be surprised by their
presence. No one will force their use and one's internal style guide (and linters) can direct whether any individual group
adopts or ignores the addition.

For those of us who prefer trailing commas, it will be both a convenience and an asset. For those against, there's no harm
done. I find it unlikely that trailing commas will naturally lead to any unsafe code or produce a net negative effect on the
language or the code-base it supports.

-- Erica

seems like a good idea!

···

On 10.03.2016, at 22:40, Jonathan Tang <jonathan.d.tang@gmail.com> wrote:

Probably my vote would be "defer", and let the other changes slated for Swift 3 & 4 work there way out. Then we could see how the language evolves in syntax, and how big a problem this is in practice, and then add trailing commas in a year or two if people still believe it's a good idea.

I see them as harmless in arrays and enums because they grow and expand depending on the needs, but I think in argument lists, it means that is supposed to match with the signature, as soon as you add a comma then you are not really matching the signature there is another parameter that is empty,. If it is a variadic function it makes even more confusing. Do we pass an empty value there?

If we allow it we are going to have dangling commas in every function. I think that we should do our best to make Swift code look good, this does not really do that, it makes it look like someone forgot something or they were lazy. Javascript, PHP and other languages encourage sloppy coding. I don’t want to do that too.

Someone made the argument that the Swift formatting commission is coming. I do think that having reasonable standards would help the overall readability and understanding in the code. We just spent a lot of time coming up with standards for naming APIs, I think the same should be done here to make it so that Swift code is generally readable and we don’t need to have obfuscation contests.

Things to consider:
• Requiring spaces after commas.
• A space before and after braces in control statements.
• Space after colon.

Things will be unlikely to codify:
• forcing to put the brace at end of the line or on next line although Swift seems to have a defacto standard of putting at end of line. As much as I would like a standard here.

Already enforced:
* space before or after a comparison. like x == y.

Also, perhaps the IDE could make following these standards automatic. So if you rearrange arguments, it would automatically add or remove the trailing comma but we don’t inflict reading that extra comma at the end of the argument list. It may not bother you but I guarantee others will be bothered by it. I am not talking about reformatting like old IDEs used to do either where every character was formatted, more like what happens when you do code completion.

- Paul

···

On Mar 10, 2016, at 3:23 PM, Rainer Brockerhoff via swift-evolution <swift-evolution@swift.org> wrote:

On 3/10/16 17:27, Erica Sadun via swift-evolution wrote:

On 10 Mar 2016, at 17:28, Radosław Pietruszewski <radexpl@gmail.com <mailto:radexpl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote:

Trailing comma is not nonsensical, there is a specific purpose in allowing it (unlike your examples), and there’s precedent in Swift already.

Trailing commas enable easier re-ordering of arguments, simplify growing or compacting argument lists, and do no harm.

Their use in arrays and dictionaries are conventional enough that experienced programmers will not be surprised by their
presence. No one will force their use and one's internal style guide (and linters) can direct whether any individual group
adopts or ignores the addition.

+1!

I became aware that trailing commas were now allowed in (Obj)C
relatively recently and use them exclusively now in lists of enums,
array and dictionary constants, and so forth.

Of course with a preprocessor they allow conditionally inserting
elements without any conditional gimcrackery or the hack of inserting a
final dummy element without a trailing comma.

Even so, optional trailing commas will no doubt be useful in Swift too.
I think specifically of commenting out (or uncommenting) elements during
debugging or exploratory programming.

For those of us who prefer trailing commas, it will be both a convenience and an asset. For those against, there's no harm
done. I find it unlikely that trailing commas will naturally lead to any unsafe code or produce a net negative effect on the
language or the code-base it supports.

Excellent argument, too.

--
Rainer Brockerhoff <rainer@brockerhoff.net>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
Solipsism Gradient - Rainer Brockerhoff’s blog

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

In working with many programming languages since
ca. 1980, there was not even one language offering this,
and I am an experienced programmer.

Ruby, JavaScript, CoffeeScript, Swift.

— Radek

···

On 10 Mar 2016, at 22:16, Ted F.A. van Gaalen via swift-evolution <swift-evolution@swift.org> wrote:

H Erica.

In working with many programming languages since
ca. 1980, there was not even one language offering this,
and I am an experienced programmer.

What do you mean with “reordering” in this context?
  1. only with manually editing ?
or
  2. With some form of source automation ?
or
  3. reason(s) unknown to me?

Btw. please note that Swift should also be accessible and usable for less experienced programmers.

Sorry, Erica, another topic, but if in what you wrote here you'd replace “trailing commas” with “for ; ;” you would have described exactly
my opinion about the for ;; statement. a bit contradicting, i think.

For those of us who prefer trailing commas, it will be both a convenience and an asset. For those against, there's no harm
done. I find it unlikely that trailing commas will naturally lead to any unsafe code or produce a net negative effect on the
language or the code-base it supports.

Please note, not meant unfriendly.

Kind regards
Ted

On 10.03.2016, at 21:27, Erica Sadun <erica@ericasadun.com> wrote:

On 10 Mar 2016, at 17:28, Radosław Pietruszewski <radexpl@gmail.com> wrote:

Trailing comma is not nonsensical, there is a specific purpose in allowing it (unlike your examples), and there’s precedent in Swift already.

Trailing commas enable easier re-ordering of arguments, simplify growing or compacting argument lists, and do no harm.

Their use in arrays and dictionaries are conventional enough that experienced programmers will not be surprised by their
presence. No one will force their use and one's internal style guide (and linters) can direct whether any individual group
adopts or ignores the addition.

For those of us who prefer trailing commas, it will be both a convenience and an asset. For those against, there's no harm
done. I find it unlikely that trailing commas will naturally lead to any unsafe code or produce a net negative effect on the
language or the code-base it supports.

-- Erica

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

C and C++, but only for enum cases and initializer lists (braced-init-lists in C++). Possibly C# as well in these cases.

Python, everywhere (AFAICT).

Jordan

···

On Mar 10, 2016, at 13:29, Radosław Pietruszewski via swift-evolution <swift-evolution@swift.org> wrote:

In working with many programming languages since
ca. 1980, there was not even one language offering this,
and I am an experienced programmer.

Ruby, JavaScript, CoffeeScript, Swift.

Perl accepted trailing commas before it was cool.

-Joe

···

On Mar 10, 2016, at 1:29 PM, Radosław Pietruszewski via swift-evolution <swift-evolution@swift.org> wrote:

In working with many programming languages since
ca. 1980, there was not even one language offering this,
and I am an experienced programmer.

Ruby, JavaScript, CoffeeScript, Swift.

Impossible, perl was always cool.

···

On Thu, Mar 10, 2016 at 4:21 PM, Joe Groff via swift-evolution < swift-evolution@swift.org> wrote:

Perl accepted trailing commas before it was cool.

>
>> In working with many programming languages since
>> ca. 1980, there was not even one language offering this,
>> and I am an experienced programmer.
>
> Ruby, JavaScript, CoffeeScript, Swift.

And from the Java specification:
"A trailing comma may appear after the last expression in an array
initializer and is ignored."
(And it's been there since JLS 1.0)

···

On Fri, Mar 11, 2016 at 1:21 AM, Joe Groff via swift-evolution < swift-evolution@swift.org> wrote:

> On Mar 10, 2016, at 1:29 PM, Radosław Pietruszewski via swift-evolution < > swift-evolution@swift.org> wrote:

On Fri, Mar 11, 2016 at 1:21 AM, Joe Groff via swift-evolution < swift-evolution@swift.org> wrote:

> On Mar 10, 2016, at 1:29 PM, Radosław Pietruszewski via swift-evolution < > swift-evolution@swift.org> wrote:
>
>> In working with many programming languages since
>> ca. 1980, there was not even one language offering this,
>> and I am an experienced programmer.
>
> Ruby, JavaScript, CoffeeScript, Swift.

Perl accepted trailing commas before it was cool.

-Joe
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden

Phone: +46-73-753 24 62
E-mail: jens@bitcycle.com