<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="">Yes, this is a bit different. There was a discussion about a month ago (<a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/17142" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/17142</a>) which had a few good ideas how to deal with the following pattern:<div class=""><br class=""></div><div class="">if let x = x { // do something with x }</div><div class="">guard let x = x { return }</div><div class=""><br class=""></div><div class="">which is shadowing the original optional value. The suggestion was:</div><div class=""><br class=""></div><div class="">if let x! { // within this block, x is no longer optional }</div><div class=""><br class=""></div><div class="">guard let x! { return }</div><div class="">// Now x is no longer an optional.</div><div class=""><br class=""></div><div class="">Generally, it re-used the same variable name to safe-unwrap the optional. In your particular example:</div><div class=""><br class=""></div><div class="">let i: Int? = nil</div><div class="">if let i! {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let y = i.toIntMax()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>/// ...</div><div class="">}</div><div class=""><br class=""></div><div class="">I am aware of the .map (or flatMap) on the optional, however, the guard statement in particular seems like an improvement.</div><div class=""><br class=""></div><div class="">There are many language constructs in Swift that can be expressed using other constructs - you can go all the way down and say that you really don't need anything other than if and goto.</div><div class=""><br class=""></div><div class="">The question is whether such a syntax sugar is something that would be help one write safer code and if it's something people would use.</div><div class=""><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 23, 2016, at 9:32 PM, David Sweeris &lt;<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Dmitri pointed out a few posts ago that Swift already has this.<div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;opInt:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? =&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">nil</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">opInt</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">map</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;{$0.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">toIntMax</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()}&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures;" class="">//Evaluates to nil</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">Are you talking about something different?</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">- Dave Sweeris</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><blockquote type="cite" class="">On Jun 23, 2016, at 2:04 PM, Charlie Monroe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Sure, the exact syntax is a matter of discussion, I just wasn't that much of favor of the very short<br class=""><br class="">doSomething(with: myOptional?)<br class=""><br class="">- it looks like a great idea, making the code really short<br class="">- on the other hand the question mark is next to the variable, but the method's execution is optional - in that sense something like doSomething(?: myOptional)(with: myOptional) makes more sense, declaring explicitely what optionals does the execution depend on.<br class="">- nevertheless, in the interest of clarity and readability of the code, I'm still in favor of the original proposal, which requires you to either use if or guard.<br class=""><br class=""><blockquote type="cite" class="">On Jun 23, 2016, at 8:57 PM, Tim Vermeulen &lt;<a href="mailto:tvermeulen@me.com" class="">tvermeulen@me.com</a>&gt; wrote:<br class=""><br class="">But ! still suggests force unwrapping, while ? suggests safe unwrapping. Why not use a question mark?<br class=""><br class=""><blockquote type="cite" class="">It was in the previous proposal and suggested that you are not trying to shadow the previous variable, but trying to unwrap it - and it acts as unwrapped from there on.<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jun 23, 2016, at 8:52 PM, Tim Vermeulen&lt;<a href="mailto:tvermeulen@me.com" class="">tvermeulen@me.com</a>&gt;wrote:<br class=""><br class="">Why with the exclamation mark? It suggests you’re force unwrapping something.<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Jun 23, 2016, at 8:45 PM, Tim Vermeulen via swift-evolution&lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;wrote:<br class=""><br class="">I would love to be able to do something like<br class=""><br class="">doSomething(with: myOptional?)<br class=""></blockquote>This actually looks good to me, though if I were a newcomer to the language, it would be really cryptic.<br class=""><br class="">In case the function returned any value, it could become an optional, just like with try?...<br class=""><br class="">I still, however, prefer the original proposal of if let myOptional! { doSomething(myOptional) }...<br class=""><br class=""><blockquote type="cite" class=""><br class="">which would be equivalent to<br class=""><br class="">if let myValue = myOptional {<br class="">doSomething(with: myValue)<br class="">}<br class=""><br class="">But it’s been discussed here before, and I don’t think people were very enthusiastic about it.<br class=""><br class=""><blockquote type="cite" class="">I was wondering if people would be open to adding an unwrap method to the Optional type,I already have a method like this which shortens code for me.<br class=""><br class="">So this:<br class=""><br class="">let myReallyLongOptionalName: String? = "Hey"<br class=""><br class="">if let string = myReallyLongOptionalName {<br class="">doSomethingWith(string)<br class="">}<br class=""><br class="">Could become"<br class=""><br class="">let myReallyLongOptionalName: String? = "Hey"<br class=""><br class="">myReallyLongOptionalName.unwrap {<br class="">doSomethingWith($0)<br class="">}<br class=""><br class="">The block would only be fired if myReallyLongOptionalName has a value.<br class=""><br class=""><br class="">___________________________________<br class=""><br class=""><br class="">James⎥Head of Trolls<br class=""><br class=""><br class=""><a href="mailto:james@supmenow.com" class="">james@supmenow.com</a>(<a href="mailto:james@supmenow.com" class="">mailto:james@supmenow.com</a>)⎥<a href="http://supmenow.com/" class="">supmenow.com</a>(<a href="http://supmenow.com/" class="">http://supmenow.com</a>)<br class=""><br class=""><br class="">Sup<br class=""><br class=""><br class="">Runway East<br class=""><br class=""><br class="">10 Finsbury Square<br class=""><br class=""><br class="">London<br class=""><br class=""><br class="">EC2A 1AF<br class=""></blockquote>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote></blockquote></blockquote><br class=""><br class=""></blockquote></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class=""></div></div></blockquote></div><br class=""></div></div></body></html>