[Review] SE-0084: Allow trailing commas in parameter lists and tuples


(Doug McKenna) #1

In all this discussion of trailing commas, there is a use-case that's not been discussed, which is whether or not a trailing comma after the last of a list of comma-separated items (typically on separate lines) makes it easier to write a program that outputs Swift code declaring that list. It does.

If such a trailing comma is going to raise a syntax error, and one writes a loop for a set of listed items that will print those listed items, one either has to special case the first item to not be preceded by a ",\n", and print all subsequent items prefixed with a ",\n"; or one has to end all items except the last with a ",\n". Either way is a pain, adds an extra test to the loop, and likely will be implemented erroneously the first time.

Code that is written by other code always has a strong incentive to avoid raising any compiler error/warnings, and so tends not to be read as much by humans who might think there's a missing item after the last extra comma.

How about a syntax warning/error for only those trailing commas that end a line that contains a previous comma serving the same syntactic function at the same syntactic level?

Doug McKenna


(Thorsten Seitz) #2

In all this discussion of trailing commas, there is a use-case that's not been discussed, which is whether or not a trailing comma after the last of a list of comma-separated items (typically on separate lines) makes it easier to write a program that outputs Swift code declaring that list. It does.

If such a trailing comma is going to raise a syntax error, and one writes a loop for a set of listed items that will print those listed items, one either has to special case the first item to not be preceded by a ",\n", and print all subsequent items prefixed with a ",\n"; or one has to end all items except the last with a ",\n". Either way is a pain, adds an extra test to the loop, and likely will be implemented erroneously the first time.

I would expect that loop to be abstracted away so that special case has to be written only once. You would probably like indentation for nesting as well in the generated code, so there is already reason to write a little helper (let’s call it IndentingWriter) or maybe use a full blown framework for pretty printing (e.g. like Haskell’s Text.PrettyPrint, see https://downloads.haskell.org/~ghc/latest/docs/html/libraries/pretty-1.1.2.0/Text-PrettyPrint.html).

-Thorsten

···

Am 11.05.2016 um 23:12 schrieb Douglas McKenna via swift-evolution <swift-evolution@swift.org>:

Code that is written by other code always has a strong incentive to avoid raising any compiler error/warnings, and so tends not to be read as much by humans who might think there's a missing item after the last extra comma.

How about a syntax warning/error for only those trailing commas that end a line that contains a previous comma serving the same syntactic function at the same syntactic level?

Doug McKenna

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