[swift-evolution] [Draft] Allow trailing commas in argument lists
grp at fb.com
Tue Mar 8 16:07:59 CST 2016
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(
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(
let tuple = (
## 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 = [
There should not be any impact to existing code from this proposal.
- PHP: https://wiki.php.net/rfc/trailing-comma-function-args
## Alternatives Considered
let person =
, 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.
More information about the swift-evolution