[swift-evolution] Spread Operator as Shorthand for Map

Matthew Johnson matthew at anandabits.com
Wed Dec 16 09:14:49 CST 2015


You can already define an infix operator that does this.  Are you proposing one be in the standard library?  I would be opposed to adding new syntax for this.

> On Dec 16, 2015, at 6:47 AM, Sean Kosanovich via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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
> 
> _______________________________________________
> 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/20151216/9a15c8d6/attachment.html>


More information about the swift-evolution mailing list