[swift-evolution] Spread Operator as Shorthand for Map

Sean Kosanovich sean7512 at me.com
Wed Dec 16 09:18:13 CST 2015


Yes, I am proposing this be part of the standard library.

Additional use cases that are possible in Groovy are using it for setting variables or calling methods, like the following (ignore the example of changing a car’s make doesn’t make much since):

cars*.make = “Chevrolet”

or 

cars*.notifyOfLowFuel()


> On Dec 16, 2015, at 10:14 AM, Matthew Johnson <matthew at anandabits.com> 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 <mailto: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 <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <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/a84ac950/attachment-0001.html>


More information about the swift-evolution mailing list