[swift-evolution] [Draft] Allow trailing commas in argument lists
Haravikk
swift-evolution at haravikk.me
Tue Mar 8 16:31:05 CST 2016
While I agree with the benefits to diffs, the main question is whether a trailing comma should represent a possible mistake or not. For example, if I have two values with two commas in a tuple did I mean that tuple to be two elements or three (forgetting to add the third)? The same issue could apply to functions, particularly if you’re dealing with functions/initialisers with multiple signatures that could lead to ambiguity.
Admittedly I like having the capability on arrays, but I do agree that it should be consistent, I’m just not sure whether removing trailing commas everywhere would be a good thing either, especially if leads to a that horrible leading comma syntax just to make diffs easier.
So I’m a +not sure at the moment, it bears some thinking about!
I wonder if it’d be possible to report the trailing comma as a mistake but only in certain cases; i.e- only if there actually is ambiguity with other function/initialiser variants, and likewise for tuples that have a known type. For example, the following would still be an error:
let foo = (a:1, b:2,) // Not sure what you meant
But this wouldn’t be:
let foo:(Int, Int) = (1, 2,) // Two for two, should be okay
I suppose this could be further enforced by only allowing the trailing comma on multi-line statements, since they don’t serve much purpose on a single line. In this case my second example would also fail, but the following wouldn’t:
let foo:(Int, Int) = (
a: 1234,
b: 4321,
)
Because it’s unambiguous, and the comma is of more use here.
I guess I’m a ±dunno at the moment, it bears some thinking about =D
> On 8 Mar 2016, at 22:07, Grant Paul via swift-evolution <swift-evolution at swift.org> wrote:
>
> ## Introduction
>
> 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,
> )
>
>
> ## Motivation
>
> It’s common for functions to take a number of arguments. In some languages, this can make it difficult to figure out what a function does, leading to patterns like fluent interfaces and configuration objects.
>
> Swift, by contrast, handles this very well. Argument labels make sure parameters aren’t confused even when they’re of the same type. And compared to Objective-C, it’s much easier to write a multi-line list of arguments in Swift.
>
> However, with a parentheses placement style placing the closing parentheses for a multi-line call on a new line, Swift does not support a trailing comma. Trailing commas have a number of benefits:
>
> - It is easier to re-arrange lines (especially in certain text editors) when all lines have commas.
> - Line-based diffs (as used by most source control systems) only show added lines when adding a new parameter to the end, rather than two lines where one just adds the comma.
> - It’s more consistent with other Swift lists which do support trailing commas.
>
>
> ## Proposed Solution
>
> The proposed solution is to allow and ignore trailing commas in argument lists and tuples:
>
> let person = Person(
> id: json['id'],
> name: json['name'],
> picture: Image(picture),
> friends: friends,
> )
>
> let tuple = (
> color,
> 32,
> )
>
>
> ## Detailed Design
>
> Support for trailing commas in argument lists and tuples would make them consistent with Swift’s handling of array literals, which do support trailing commas:
>
> let array = [
> 2,
> 4,
> 8,
> ]
>
> There should not be any impact to existing code from this proposal.
>
> Support for this syntax is also found in other programming languages like Python, D, and Hack. It’s been proposed for JavaScript (positive response) and PHP (rejected):
>
> - JavaScript: https://jeffmo.github.io/es-trailing-function-commas/
> - PHP: https://wiki.php.net/rfc/trailing-comma-function-args
>
>
> ## Alternatives Considered
>
> The main alternative is the existing behavior. This has the benefit of standardizing Swift code on a particular style. However, many people will in practice continue to use a style regardless of support trailing commas, especially for cross-language consistency. It could also lead to JavaScript-inspired parameter ordering:
>
> let person =
> Person(id: json['id']
> , name: json['name']
> , picture: Image(picture)
> , friends: friends)
>
> Another alternative would be to support this syntax for function parameters but not tuples. However, this would be an arbitrary inconsistency.
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list