[swift-evolution] Spread Operator as Shorthand for Map

ilya ilya.nikokoshev at gmail.com
Wed Dec 16 09:21:49 CST 2015


I think you would be able to define such an operator if .make was
recognized as Car.make in the Car context.
Which would be a cool addition to current rules.

On Wed, Dec 16, 2015 at 18:15 Matthew Johnson via swift-evolution <
swift-evolution at swift.org> wrote:

> 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
>
>
> _______________________________________________
> 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/65c2e833/attachment.html>


More information about the swift-evolution mailing list