[swift-evolution] Add an ifPresent function to Optional

Adrian Kashivskyy adrian.kashivskyy at me.com
Fri Mar 11 11:41:19 CST 2016


This only solves the problem if the target function has one argument. I don't think the problem is so broad that is requires a stdlib change.

I'm -1 on this for now.


Pozdrawiam – Regards,
Adrian Kashivskyy

> Wiadomość napisana przez Brian Pratt via swift-evolution <swift-evolution at swift.org> w dniu 11.03.2016, o godz. 16:28:
> 
> +1, feels like a no-brainer and is a nice low-impact change. 
> 
> I like it.
> 
> On Fri, Mar 11, 2016 at 12:14 AM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> +1
> ifPresent is something I've used a lot in Java/Guava.
> In Smalltalk we had at:ifPresent: for dictionary lookup which could then easily be simulated by lookup followed by ifPresent.
> 
> -Thorsten 
> 
> Am 10.03.2016 um 22:55 schrieb Lammert Westerhoff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
> 
>> Currently the Swift Optional doesn’t have an ifPresent function. 
>> This function is much like the map function except that the passed in function always returns Void. Almost the same can be done with the mapfunction, however the map function gives a compiler warning if it's result is unused. Also a map function can be ambiguous when there are multiple functions with the same name. The ifPresent will always pick the right function; i.e. the one returning Void and therefore is not ambiguous .
>> 
>> The ifPresent function is like the map function very powerful. Some examples of its usage:
>> 
>> let mapView: MKMapView = ... // some map view
>> let annotation: MKAnnotation? = ... // some optional annotation
>> annotation.ifPresent(mapView.addAnnotation)
>> 
>> I also wrote a Blog Post about this topic in which I present it as an extension of Optional with some more examples where I find it very useful: https://swiftforward.wordpress.com/2015/12/04/add-ifpresent-to-swift-optionals/ <https://swiftforward.wordpress.com/2015/12/04/add-ifpresent-to-swift-optionals/>
>> 
>> The implementation can be as simple as following:
>> 
>> public func ifPresent(@noescape f: (Wrapped) throws -> Void) rethrows {
>>    _ = try map(f)
>> }
>> 
>>>> Lammert Westerhoff
>> _______________________________________________
>> 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>
> 
> _______________________________________________
> 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>
> 
> 
> _______________________________________________
> 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/20160311/03f4e272/attachment.html>


More information about the swift-evolution mailing list