<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 10, 2016, at 2:55 PM, Lammert Westerhoff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Currently the Swift Optional doesn’t have an ifPresent function.&nbsp;<br class="">This function is much like the&nbsp;map&nbsp;function except that the passed in function always returns&nbsp;Void. Almost the same can be done with the&nbsp;mapfunction, however the&nbsp;map&nbsp;function gives a&nbsp;compiler warning if it's result is unused. Also a&nbsp;map&nbsp;function can be ambiguous when there are multiple functions with the same name. The&nbsp;ifPresent&nbsp;will always pick the right function; i.e.&nbsp;the one returning&nbsp;Void&nbsp;and therefore is not ambiguous .<br class=""><br class="">The&nbsp;ifPresent&nbsp;function is like the&nbsp;map&nbsp;function very powerful. Some examples of its usage:<br class=""><br class=""><div class="">let mapView: MKMapView = ... // some map view</div><div class="">let annotation: MKAnnotation? = ... // some optional annotation</div><div class="">annotation.ifPresent(mapView.addAnnotation)</div><br class="Apple-interchange-newline">I also wrote a&nbsp;Blog Post&nbsp;about this topic in which I present it as an extension of&nbsp;Optional with some more examples where I find it very useful:&nbsp;<a href="https://swiftforward.wordpress.com/2015/12/04/add-ifpresent-to-swift-optionals/" class="">https://swiftforward.wordpress.com/2015/12/04/add-ifpresent-to-swift-optionals/</a><div class=""><br class=""></div></div></div></blockquote><br class=""></div><div>While I'm not convinced of the utility or lack thereof, wouldn't it make more sense&nbsp;</div><div>to extend `forEach` to support optionals in a similar way that map and flapMap&nbsp;</div><div>currently do rather than introduce `ifPresent`?</div><div><br class=""></div><div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> y: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> n: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">_</span>&nbsp;=&nbsp;<span style="color: rgb(79, 129, 135);" class="">y</span>.<span style="color: rgb(61, 29, 129);" class="">map</span>({&nbsp;<span style="color: rgb(61, 29, 129);" class="">print</span>($0) }) // works in current Swift</div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">_</span>&nbsp;=&nbsp;<span style="color: rgb(79, 129, 135);" class="">n</span>.<span style="color: rgb(61, 29, 129);" class="">map</span>({&nbsp;<span style="color: rgb(61, 29, 129);" class="">print</span>($0) }) // ditto</div></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class=""><br class=""></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">y</span>.forEach{ print($0) } // not currently a thing</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">n</span>.forEach{ print($0) } // ditto</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><div style="font-family: Palatino; font-size: 14px;">Just spitballing here.</div><div style="font-family: Palatino; font-size: 14px;"><br class=""></div><div style="font-family: Palatino; font-size: 14px;">-- E</div><div style="font-family: Palatino; font-size: 14px;"><br class=""></div></div></div></body></html>