<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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. <br class="">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 class=""><br class="">The ifPresent function is like the map 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 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/" 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 </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 </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"> </span>y:<span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Int</span>? =<span class="Apple-converted-space"> </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"> </span>n:<span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Int</span>? =<span class="Apple-converted-space"> </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> = <span class="" style="color: rgb(79, 129, 135);">y</span>.<span class="" style="color: rgb(61, 29, 129);">map</span>({ <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> = <span class="" style="color: rgb(79, 129, 135);">n</span>.<span class="" style="color: rgb(61, 29, 129);">map</span>({ <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>