[swift-evolution] Spread Operator as Shorthand for Map
Andrew Bennett
cacoyi at gmail.com
Wed Dec 16 08:21:50 CST 2015
That's a pretty nifty shorthand, although I don't personally think it adds
enough on top of map to be worthwhile:
let makes = cars*.make
let makes = cars.map{$0.make}
My reasons:
* It adds new things to learn, and only saves 4 characters on map
* Perhaps I'm missing your point, but your code example is also null safe,
this doesn't seem to add an advantage.
On Wed, Dec 16, 2015 at 11:47 PM, 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/20151217/80a7efee/attachment.html>
More information about the swift-evolution
mailing list