[swift-evolution] Spread Operator as Shorthand for Map

Sean Kosanovich sean7512 at me.com
Wed Dec 16 06:47:35 CST 2015


As a Groovy user, I really enjoy the shorthand Spread Operator over the Collect Closure (Map is the equivalent in Swift).

Consider the following Swift code:

struct Car {
    let make: String
    let model: String
}

let cars = [Car(make: "Jeep", model: "Grand Cherokee"), Car(make: "Dodge", model: "Challenger")]
let makes = cars.map() { $0.make }

 

Now consider the same code using a Spread Operator:

let makes = cars*.make


The other distinction in Groovy is that the Spread Operator is null safe, meaning it won’t throw a NPE if an element is null, whereas the Collect Closure would.  So in Swift, I would propose the Spread Operator implicitly does this when operating on an array of Optionals:

let makes = cars.map() { $0?.make }


Thanks!
Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/28037599/attachment.html>


More information about the swift-evolution mailing list