<div dir="ltr">+1, feels like a no-brainer and is a nice low-impact change. <div><br></div><div>I like it.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 11, 2016 at 12:14 AM, Thorsten Seitz via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div></div><div>+1</div><div>ifPresent is something I've used a lot in Java/Guava.</div><div>In Smalltalk we had at:ifPresent: for dictionary lookup which could then easily be simulated by lookup followed by ifPresent.</div><div><br></div><div>-Thorsten </div><div><div class="h5"><div><br>Am 10.03.2016 um 22:55 schrieb Lammert Westerhoff via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div>Currently the Swift Optional doesn’t have an ifPresent function. <br>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 .<br><br>The ifPresent function is like the map function very powerful. Some examples of its usage:<br><br><div>let mapView: MKMapView = ... // some map view</div><div>let annotation: MKAnnotation? = ... // some optional annotation</div><div>annotation.ifPresent(mapView.addAnnotation)</div><br>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: <a href="https://swiftforward.wordpress.com/2015/12/04/add-ifpresent-to-swift-optionals/" target="_blank">https://swiftforward.wordpress.com/2015/12/04/add-ifpresent-to-swift-optionals/</a><div><br></div><div>The implementation can be as simple as following:</div><div><br>public func ifPresent(@noescape f: (Wrapped) throws -> Void) rethrows {<br> _ = try map(f)<br>}</div><div><br></div><div>—</div><div>Lammert Westerhoff</div></div></blockquote></div></div><blockquote type="cite"><div><span>_______________________________________________</span><span class=""><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></span></div></blockquote></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>