[Draft] Allow trailing commas in argument lists


(Grant Paul) #1

## 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.


Roadmap for and state of single element tuple types?
(Jordan Rose) #2

+1, thanks for bringing it up.

···

On Mar 8, 2016, at 14:07, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Erica Sadun) #3

I like trailing commas in all the things, up to and including variadic arguments.

-- E

···

On Mar 8, 2016, at 3:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Joe Groff) #4

Yes please.

···

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Haravikk) #5

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) = (
    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

···

a: 1234,

On 8 Mar 2016, at 22:07, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charles Kissinger) #6

+1. Although a minor point, it would make the writing of “code that generates code” a little easier, too.

I think I kind of like Haravikk’s idea of only allowing it on multi-line argument lists, though. It should probably at least be an alternative in the proposal.

-1 for mandatory spaces (not a part of this proposal). That should be in a linter IMO.

—CK

···

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Nisse Bergman) #7

-1
Let’s not go down this path and enable the javascript flamewar of trailing or non-trailing commas.

Nisse

···

On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

Yes please.

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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


(E. Maloney) #8

It feels intuitively wrong to me to change the language so that it accepts incorrect syntax just to make editing/merging/diffing easier.

A trailing comma is as likely to be a mistake as it is some sort of deliberately-taken convenience shortcut. Sometimes I deliberately leave unfinished expressions in place when I go off to do something else, knowing that by hitting ⌘-B the compiler will bring me right back to where I was when I return. I like that the Swift compiler is good at catching my mistakes.

Anyway, I'd bet that for most developers, a vast majority of merge pain is caused by Xcode project files and not commas. If the goal is to address merge pain during development, that would be the place I'd start.

That said, allowing whitespace between elements to act as a delimiter equivalent to a comma would seem to address the original concern, without leaving unsightly dangling commas at the end of things.

···

On Mar 8, 2016, at 5:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Thorsten Seitz) #9

-1
As code is read much more than it is written or diffed, I prefer its punctuation not to be misleading over saving a few keystrokes (and yes, I do not like them either, but I would like dangling commas even less).

-Thorsten

···

Am 08.03.2016 um 23:07 schrieb Grant Paul via swift-evolution <swift-evolution@swift.org>:

## 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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


#10

I'm against this proposal.

In Swift, like many other programming languages,
there are several purposes for function calling.

* Definition
    Person(name: "Daniel", age: 28, address: "...")
* Query
    obj.contains(id: "ABC001", inBox: .Blue)
* Operation
    obj.tune(toChannel: "CBS", using: .RCU)

For me, trailing comma for definition is OK:

    Person(
      name: "Daniel",
      age: 28,
      countryCode: "US",
    )

But not for others:

    obj.contains(
      id: "ABC001",
      inBox: .Blue,
    )

    tv.tune(
      toChannel: "CBS",
      using: .RCU,
    )

As an alternative, I propose: "Allow omitting commas in function parameters"
Like Objective-C, but only for multi-line.

# OK
    fn(x: 1, y: 2, z: 3)
    fn(x: 1
        y: 2, z: 3
    )
    fn(x: 1, y: 2
        z: 3
    )
    fn(
        x: 1
        y: 2
        z: 3
    )
    fn(x: 1
        y: 2, z: 3)

# NG
    fn(x: 1 y: 2 z: 3)
    fn(x: 1, y: 2, z: 3,)
    fn(
        x: 1,
        y: 2,
        z: 3,
    )

- It is easier to re-arrange lines
- Line-based diffs
- Easy to read

I don't think it's consistent with other parts in Swift,
but I also don't think function parameters are "list" or "dictionary".
It's more like "Who, what, when, where, why".

Could you please add this in your "Alternatives Considered" list? :slight_smile:

···

2016-03-09 7:07 GMT+09:00 Grant Paul via swift-evolution <swift-evolution@swift.org>:

## 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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tim Schmelter) #11

-1

If we agree with the Swift API Guidelines: "Clarity at the point of use is
your most important goal", it seems to me that adding trailing commas to a
method/function signature reduces clarity significantly, with others on
this thread and the previous articulating the supporting reasons.

As an aside: I also eschew trailing commas in array declarations, but
recognized that this convention is well established in other languages, and
am resigned to letting the linter handle that case. I would look to do that
in this case as well.

--T

···

On Tue, Mar 8, 2016 at 2:07 PM, Grant Paul via swift-evolution < swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jens Persson) #12

+1, I don't remember ever making a mistake (in a trailing-comma-allowing
language) that would have been caught if trailing commas had been
considered an error. But I do remember editing commas a lot in languages in
which trailing commas are considered an error.

(I would also be in favor of whitespace-separated elements (instead of the
current comma+optionalButAlmostAlwaysInPracticeWhitespace-separated
elements), but I'm 99% sure that would never be accepted by the community.)

···

On Wed, Mar 9, 2016 at 1:54 PM, Nisse Bergman via swift-evolution < swift-evolution@swift.org> wrote:

-1
Let’s not go down this path and enable the javascript flamewar of trailing
or non-trailing commas.

Nisse

> On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution < > swift-evolution@swift.org> wrote:
>
> Yes please.
>
>> On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution < > swift-evolution@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@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

--
bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden
http://www.bitcycle.com/
Phone: +46-73-753 24 62
E-mail: jens@bitcycle.com


(Radek Pietruszewski) #13

JavaScript’s common convention (and weird looking!) to use leading commas is exactly because it can’t do trailing commas.

Swift already allows trailing commas in arrays. Even if not everyone will know or use this, I see zero harm in allowing trailing commas in argument lists and tuples.

+1. Because Why Not™.

— Radek

···

On 09 Mar 2016, at 13:54, Nisse Bergman via swift-evolution <swift-evolution@swift.org> wrote:

-1
Let’s not go down this path and enable the javascript flamewar of trailing or non-trailing commas.

Nisse

On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

Yes please.

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

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


(Brent Royal-Gordon) #14

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)?

I think our type system is strong enough that most cases will be caught. Even if you assign a tuple through type inference, sooner or later you will pass it into something typed and it will fail to work properly.

The same issue could apply to functions, particularly if you’re dealing with functions/initialisers with multiple signatures that could lead to ambiguity.

For the same reason, I don't really worry about overloads. What I'm not sure of with functions, though, is what's the use case? Function arguments aren't typically interchangeable, and most arguments include labels which would have to be corrected anyway. I don't see a lot of downside to supporting it in a parameter list, but I don't see much upside, either.

···

--
Brent Royal-Gordon
Architechies


(Rainer Brockerhoff) #15

I continue to be in favor of trailing commas in arrays and enums, but
agree that for method/functions signatures and calls it's not so good.

However, in analogy to `;`s to separate statements on the same line, it
might be useful to consider commas in a similar light; that is, commas
would be required to separate arguments on the same line, but become
optional in multi-line statements. So:

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

···

On 3/14/16 12:08, Tim Schmelter via swift-evolution wrote:

-1

If we agree with the Swift API Guidelines: "Clarity at the point of use is
your most important goal", it seems to me that adding trailing commas to a
method/function signature reduces clarity significantly, with others on
this thread and the previous articulating the supporting reasons.

As an aside: I also eschew trailing commas in array declarations, but
recognized that this convention is well established in other languages, and
am resigned to letting the linter handle that case. I would look to do that
in this case as well.

--T

On Tue, Mar 8, 2016 at 2:07 PM, Grant Paul via swift-evolution < > swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

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


(David Owens II) #16

+1 to support trailing commas

+1 to just whitespace too.

···

Sent from my iPhone

On Mar 9, 2016, at 5:18 AM, Jens Persson via swift-evolution <swift-evolution@swift.org> wrote:

+1, I don't remember ever making a mistake (in a trailing-comma-allowing language) that would have been caught if trailing commas had been considered an error. But I do remember editing commas a lot in languages in which trailing commas are considered an error.

(I would also be in favor of whitespace-separated elements (instead of the current comma+optionalButAlmostAlwaysInPracticeWhitespace-separated elements), but I'm 99% sure that would never be accepted by the community.)

On Wed, Mar 9, 2016 at 1:54 PM, Nisse Bergman via swift-evolution <swift-evolution@swift.org> wrote:
-1
Let’s not go down this path and enable the javascript flamewar of trailing or non-trailing commas.

Nisse

> On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:
>
> Yes please.
>
>> On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

--
bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden
http://www.bitcycle.com/
Phone: +46-73-753 24 62
E-mail: jens@bitcycle.com

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


(Brentley Jones) #17

+1

···

On Wed, Mar 9, 2016 at 7:18 AM Jens Persson via swift-evolution < swift-evolution@swift.org> wrote:

+1, I don't remember ever making a mistake (in a trailing-comma-allowing
language) that would have been caught if trailing commas had been
considered an error. But I do remember editing commas a lot in languages in
which trailing commas are considered an error.

(I would also be in favor of whitespace-separated elements (instead of the
current comma+optionalButAlmostAlwaysInPracticeWhitespace-separated
elements), but I'm 99% sure that would never be accepted by the community.)

On Wed, Mar 9, 2016 at 1:54 PM, Nisse Bergman via swift-evolution < > swift-evolution@swift.org> wrote:

-1
Let’s not go down this path and enable the javascript flamewar of
trailing or non-trailing commas.

Nisse

> On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution < >> swift-evolution@swift.org> wrote:
>
> Yes please.
>
>> On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution < >> swift-evolution@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@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

--
bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden
http://www.bitcycle.com/
Phone: +46-73-753 24 62
E-mail: jens@bitcycle.com

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


(Dave Abrahams) #18

JavaScript’s common convention (and weird looking!) to use leading
commas is exactly because it can’t do trailing commas.

The reason I've seen for leading commas is that to add a new element to
the end of a list, you only need to change one line instead of two.

···

on Wed Mar 09 2016, Radosław Pietruszewski <swift-evolution@swift.org> wrote:

Swift already allows trailing commas in arrays. Even if not everyone
will know or use this, I see zero harm in allowing trailing commas in
argument lists and tuples.

+1. Because Why Not™.

— Radek

On 09 Mar 2016, at 13:54, Nisse Bergman via swift-evolution <swift-evolution@swift.org> wrote:

-1
Let’s not go down this path and enable the javascript flamewar of trailing or non-trailing commas.

Nisse

On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

Yes please.

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

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

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

--
-Dave


(Adrian Kashivskyy) #19

I see the following an advantage of using trailing commas:

If using trailing commas, when adding an item at the end of array, only one line has to be changed (added). If not, two lines are diffed (comma + new element).

So yeah, +1. Because Why Not™. ¯\_(ツ)_/¯

Pozdrawiam – Regards,
Adrian Kashivskyy

···

Wiadomość napisana przez Radosław Pietruszewski via swift-evolution <swift-evolution@swift.org> w dniu 09.03.2016, o godz. 15:02:

JavaScript’s common convention (and weird looking!) to use leading commas is exactly because it can’t do trailing commas.

Swift already allows trailing commas in arrays. Even if not everyone will know or use this, I see zero harm in allowing trailing commas in argument lists and tuples.

+1. Because Why Not™.

— Radek

On 09 Mar 2016, at 13:54, Nisse Bergman via swift-evolution <swift-evolution@swift.org> wrote:

-1
Let’s not go down this path and enable the javascript flamewar of trailing or non-trailing commas.

Nisse

On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

Yes please.

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

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

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


(Radek Pietruszewski) #20

JavaScript’s common convention (and weird looking!) to use leading
commas is exactly because it can’t do trailing commas.

The reason I've seen for leading commas is that to add a new element to
the end of a list, you only need to change one line instead of two.

Well, yeah — that’s exactly why enabling (or… not banning?) trailing commas is nice. You can easily add new lines, reorder them, etc., and you don’t have to worry about the last line.

The difference is, leading commas don’t require any special support, but I think most people would agree, a trailing comma is much more natural looking.

— Radek

···

Swift already allows trailing commas in arrays. Even if not everyone
will know or use this, I see zero harm in allowing trailing commas in
argument lists and tuples.

+1. Because Why Not™.

— Radek

On 09 Mar 2016, at 13:54, Nisse Bergman via swift-evolution <swift-evolution@swift.org> wrote:

-1
Let’s not go down this path and enable the javascript flamewar of trailing or non-trailing commas.

Nisse

On 09 Mar 2016, at 01:23, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

Yes please.

On Mar 8, 2016, at 2:07 PM, Grant Paul via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

_______________________________________________
swift-evolution mailing list
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

--
-Dave

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