<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div style="direction: inherit;">I prefer the much simpler and natural:</div><div style="direction: inherit;"><br></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">let value = x.map { foo.bar(x: $0) }</span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">Is it me or has everybody been avoiding the map solution for some reason. It's the most elegant way to do such operations IMHO.</span></div></div><div><br>On 15 Aug 2016, at 22:05, Charles Srstka via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><blockquote type="cite" class="">On Aug 15, 2016, at 2:27 PM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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="">`let value = (x == nil) ? nil : foo.bar(x: x)` isn't so bad, is it? You could even write a custom operator to sugar it.</span></div></blockquote></div><br class=""><div class="">It’s distasteful, due to the need to use the force-unwrap operator. In cases like this, I usually end up writing:</div><div class=""><br class=""></div><div class="">let value: Foo? = nil</div><div class=""><br class=""></div><div class="">if let x = x {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>value = foo.bar(x: x)</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>value = nil</div><div class="">}</div><div class=""><br class=""></div><div class="">or:</div><div class=""><br class=""></div><div class="">let value: Foo? = {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if let x = x {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return foo.bar(x: x)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return nil</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}()</div><div class=""><br class=""></div><div class="">Both of which are unwieldy, but necessary to avoid the use of !.</div><div class=""><br class=""></div><div class="">I wouldn’t mind something like an overload on the ternary operator:</div><div class=""><br class=""></div><div class="">let value = x? ? foo.bar(x: x) : nil</div><div class=""><br class=""></div><div class="">in which a ? after the ternary condition indicates that it is an optional to be unwrapped for the positive condition.</div><div class=""><br class=""></div><div class="">Charles</div><div class=""><br class=""></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>