[swift-evolution] Carriage Return in Collection Array Literal Initializer

Ross O'Brien narrativium+swift at gmail.com
Sun Apr 24 16:40:12 CDT 2016


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 at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160424/51fce714/attachment.html>


More information about the swift-evolution mailing list