Carriage Return in Collection Array Literal Initializer


(Ivan Oparin) #1

Sometimes in is necessary to initialize a collection with a long list of items. Consider this example:

let dictionaryWithALotOfItems = ["someString0" : SomeFunctionality.0, "someString1" : SomeFunctionality.0, "someString2" : SomeFunctionality.0, "someString3" : SomeFunctionality.1, "someString4" : SomeFunctionality.1]

items are separated with a comma, and we can ommit it for the last item.

However, sometimes we deal with longer collections and we can make the code more readable by writing each item on it's own line:

let dictionaryWithALotOfItems = [
    "someString0" : SomeFunctionality.0,
    "someString1" : SomeFunctionality.0,
    "someString2" : SomeFunctionality.0,
    "someString3" : SomeFunctionality.1,
    "someString4" : SomeFunctionality.1
]

now it looks much better except the comma in the end of line does not look natural

that is espessialy true for the line with a closure:
    "someString5" : SomeFunctionality.2 { some functionality },

some closures are longer:
    "someString6" : SomeFunctionality.3 {
        some
        long
        closure
        functionality
    },

if Swift could treat carriage return in array literal initializer as a separation for the items, that would make some collections look cleaner:

let dictionaryWithALotOfItems = [
    "someString0" : SomeFunctionality.0
    "someString1" : SomeFunctionality.0
    "someString2" : SomeFunctionality.0
    "someString3" : SomeFunctionality.1
    "someString4" : SomeFunctionality.1
]

just like a line without a semicolon looks better


(Ross O'Brien) #2

There was a discussion some time ago about making the comma of the last
entry optional, because adding entries meant changing more than one line of
code and this annoyed users of version control systems. This is an elegant
approach to that problem.
I don't know if it's a practical approach for the compiler, but if it is
then I'm in favour.

···

On Sat, Apr 23, 2016 at 11:27 PM, Ivan Oparin via swift-evolution < swift-evolution@swift.org> wrote:

Sometimes in is necessary to initialize a collection with a long list of
items. Consider this example:

let dictionaryWithALotOfItems = ["someString0" : SomeFunctionality.0,
"someString1" : SomeFunctionality.0, "someString2" : SomeFunctionality.0,
"someString3" : SomeFunctionality.1, "someString4" : SomeFunctionality.1]

items are separated with a comma, and we can ommit it for the last item.

However, sometimes we deal with longer collections and we can make the
code more readable by writing each item on it's own line:

let dictionaryWithALotOfItems = [
    "someString0" : SomeFunctionality.0,
    "someString1" : SomeFunctionality.0,
    "someString2" : SomeFunctionality.0,
    "someString3" : SomeFunctionality.1,
    "someString4" : SomeFunctionality.1
]

now it looks much better except the comma in the end of line does not look
natural

that is espessialy true for the line with a closure:
    "someString5" : SomeFunctionality.2 { some functionality },

some closures are longer:
    "someString6" : SomeFunctionality.3 {
        some
        long
        closure
        functionality
    },

if Swift could treat carriage return in array literal initializer as a
separation for the items, that would make some collections look cleaner:

let dictionaryWithALotOfItems = [
    "someString0" : SomeFunctionality.0
    "someString1" : SomeFunctionality.0
    "someString2" : SomeFunctionality.0
    "someString3" : SomeFunctionality.1
    "someString4" : SomeFunctionality.1
]

just like a line without a semicolon looks better
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Kenny Leung) #3

This is brilliant! If semicolons are optional, why not commas?

-Kenny

···

On Apr 24, 2016, at 2:40 PM, Ross O'Brien via swift-evolution <swift-evolution@swift.org> wrote:

There was a discussion some time ago about making the comma of the last entry optional, because adding entries meant changing more than one line of code and this annoyed users of version control systems. This is an elegant approach to that problem.
I don't know if it's a practical approach for the compiler, but if it is then I'm in favour.

On Sat, Apr 23, 2016 at 11:27 PM, Ivan Oparin via swift-evolution <swift-evolution@swift.org> wrote:
Sometimes in is necessary to initialize a collection with a long list of items. Consider this example:

let dictionaryWithALotOfItems = ["someString0" : SomeFunctionality.0, "someString1" : SomeFunctionality.0, "someString2" : SomeFunctionality.0, "someString3" : SomeFunctionality.1, "someString4" : SomeFunctionality.1]

items are separated with a comma, and we can ommit it for the last item.

However, sometimes we deal with longer collections and we can make the code more readable by writing each item on it's own line:

let dictionaryWithALotOfItems = [
    "someString0" : SomeFunctionality.0,
    "someString1" : SomeFunctionality.0,
    "someString2" : SomeFunctionality.0,
    "someString3" : SomeFunctionality.1,
    "someString4" : SomeFunctionality.1
]

now it looks much better except the comma in the end of line does not look natural

that is espessialy true for the line with a closure:
    "someString5" : SomeFunctionality.2 { some functionality },

some closures are longer:
    "someString6" : SomeFunctionality.3 {
        some
        long
        closure
        functionality
    },

if Swift could treat carriage return in array literal initializer as a separation for the items, that would make some collections look cleaner:

let dictionaryWithALotOfItems = [
    "someString0" : SomeFunctionality.0
    "someString1" : SomeFunctionality.0
    "someString2" : SomeFunctionality.0
    "someString3" : SomeFunctionality.1
    "someString4" : SomeFunctionality.1
]

just like a line without a semicolon looks better
_______________________________________________
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


(Alex Martini) #4

A comma is already allowed after the last element in an array or dictionary literal:

Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29). Type :help for assistance.
  1> let x = [1, 2, 3,]
x: [Int] = 3 values {
  [0] = 1
  [1] = 2
  [2] = 3
}
  2> let y = [1: "one", 2: "two", 3: "three",]
y: [Int : String] = 3 key/value pairs {
  [0] = {
    key = 2
    value = "two"
  }
  [1] = {
    key = 3
    value = "three"
  }
  [2] = {
    key = 1
    value = "one"
  }
}

···

On Apr 25, 2016, at 9:25 AM, Kenny Leung via swift-evolution <swift-evolution@swift.org> wrote:

This is brilliant! If semicolons are optional, why not commas?

-Kenny

On Apr 24, 2016, at 2:40 PM, Ross O'Brien via swift-evolution <swift-evolution@swift.org> wrote:

There was a discussion some time ago about making the comma of the last entry optional, because adding entries meant changing more than one line of code and this annoyed users of version control systems. This is an elegant approach to that problem.
I don't know if it's a practical approach for the compiler, but if it is then I'm in favour.

On Sat, Apr 23, 2016 at 11:27 PM, Ivan Oparin via swift-evolution <swift-evolution@swift.org> wrote:
Sometimes in is necessary to initialize a collection with a long list of items. Consider this example:

let dictionaryWithALotOfItems = ["someString0" : SomeFunctionality.0, "someString1" : SomeFunctionality.0, "someString2" : SomeFunctionality.0, "someString3" : SomeFunctionality.1, "someString4" : SomeFunctionality.1]

items are separated with a comma, and we can ommit it for the last item.

However, sometimes we deal with longer collections and we can make the code more readable by writing each item on it's own line:

let dictionaryWithALotOfItems = [
   "someString0" : SomeFunctionality.0,
   "someString1" : SomeFunctionality.0,
   "someString2" : SomeFunctionality.0,
   "someString3" : SomeFunctionality.1,
   "someString4" : SomeFunctionality.1
]

now it looks much better except the comma in the end of line does not look natural

that is espessialy true for the line with a closure:
   "someString5" : SomeFunctionality.2 { some functionality },

some closures are longer:
   "someString6" : SomeFunctionality.3 {
       some
       long
       closure
       functionality
   },

if Swift could treat carriage return in array literal initializer as a separation for the items, that would make some collections look cleaner:

let dictionaryWithALotOfItems = [
   "someString0" : SomeFunctionality.0
   "someString1" : SomeFunctionality.0
   "someString2" : SomeFunctionality.0
   "someString3" : SomeFunctionality.1
   "someString4" : SomeFunctionality.1
]

just like a line without a semicolon looks better
_______________________________________________
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


(Erica Sadun) #5

And is very appreciated!! I wish the same courtesy were extended to parameter lists.

:slight_smile:

-- E

···

On Apr 27, 2016, at 3:00 PM, Alex Martini via swift-evolution <swift-evolution@swift.org> wrote:

A comma is already allowed after the last element in an array or dictionary literal:


(Rainer Brockerhoff) #6

In general, I wish that in all such situations — when the parser expects
_either_ a comma or a closing token — it would be acceptable to use a
line break instead of the comma, or _both_ comma and closing token.

In analogy to statements, which can be delimited by semicolons or line
breaks.

···

On 4/28/16 00:00, Erica Sadun via swift-evolution wrote:

On Apr 27, 2016, at 3:00 PM, Alex Martini via swift-evolution <swift-evolution@swift.org> wrote:
A comma is already allowed after the last element in an array or dictionary literal:

And is very appreciated!! I wish the same courtesy were extended to parameter lists.

--
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/