<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=""><div class="">A couple other ideas:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Not sure about the "if" prefix, but reads nicely</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>foo.ifSome { print($0) }</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Goes with other stdlib functions accepting closures, but perhaps less clear that the closure may not execute</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>foo.withSome { print($0) }</div><div class=""><br class=""></div><div class="">–Adam</div><br class=""><div><blockquote type="cite" class=""><div class="">On 11 Mar 2016, at 12:32 PM, Erica Sadun 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=""><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">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=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">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 style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">While I'm not convinced of the utility or lack thereof, wouldn't it make more sense&nbsp;</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">to extend `forEach` to support optionals in a similar way that map and flapMap&nbsp;</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">currently do rather than introduce `ifPresent`?</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>y:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Int</span>? =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(39, 42, 216);">1</span></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>n:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Int</span>? =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">nil</span></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><br class=""></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">_</span>&nbsp;=&nbsp;<span class="" style="color: rgb(79, 129, 135);">y</span>.<span class="" style="color: rgb(61, 29, 129);">map</span>({&nbsp;<span class="" style="color: rgb(61, 29, 129);">print</span>($0) }) // works in current Swift</div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">_</span>&nbsp;=&nbsp;<span class="" style="color: rgb(79, 129, 135);">n</span>.<span class="" style="color: rgb(61, 29, 129);">map</span>({&nbsp;<span class="" style="color: rgb(61, 29, 129);">print</span>($0) }) // ditto</div></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(79, 129, 135);"><br class=""></span></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(79, 129, 135);">y</span>.forEach{ print($0) } // not currently a thing</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(79, 129, 135);">n</span>.forEach{ print($0) } // ditto</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><br class=""></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><div style="font-family: Palatino; font-size: 14px;" class="">Just spitballing here.</div><div style="font-family: Palatino; font-size: 14px;" class=""><br class=""></div><div style="font-family: Palatino; font-size: 14px;" class="">-- E</div><div style="font-family: Palatino; font-size: 14px;" class=""><br class=""></div></div></div><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>