Swift: make commas unnecessary in multi-line array/dictionary

Originator:pixelneststudio
Number:rdar://17286478 Date Originated:2014-06-12
Status:Open Resolved:
Product:Xcode Product Version:6.0 (6A215l)
Classification:Developer Tools Reproducible:Always
 
Summary:
With Swift, you have to declare a multi-line array/dictionary like this:

var example = [
  "a",
  "b",
  "c"
]

var example2 = [
  "a": "foo",
  "b": "bar",
  "c": "baz"
]

The trailing commas should be unnecessary. It should be correct and less error-prone to write:

var example = [
  "a"
  "b"
  "c"
]

var example2 = [
  "a": "foo"
  "b": "bar"
  "c": "baz"
]

By doing so, it's also more practical for versioning tool's diffs.

Additionally, this makes the comma behavior identical to the semicolon behavior (mandatory for one-liner, optional for multi-line).

Of course, for an array/dictionary one-liner, commas must be used to separate each value, like it is currently:

var example = ["a", "b", "c"]
var example2 = ["a": "foo", "b": "bar", "c": "baz"]

Steps to Reproduce:
1. Create a multi-line array/dictionary without comma, like:

var example = [
  "a"
  "b"
  "c"
]

or:

var example2 = [
  "a": "foo"
  "b": "bar"
  "c": "baz"
]

Expected Results:
No error.

Actual Results:
Compile-time error: Expected "," separator.

Version:
Xcode 6 Beta-1

Comments

No, commas should be unnecessary for single-line arrays and dictionaries as well

I like this but it does not go far enough. Commas should be unnecessary for single-line arrays and dictionaries as well.

var example = ["a" "b" "c"] var example = [1 2 3 4] var example = ["a":1 "b":2 "c":3]

are all perfectly unambiguous.

Basically, commas should be treated as whitespace within an array or dictionary literal.

By alexisgallagher at June 12, 2014, 4:17 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!