<div dir="ltr">One problem with dropping <font face="monospace, monospace">if/guard let</font><font face="arial, helvetica, sans-serif"> is something like this:</font><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="monospace, monospace">if let x = a?.b.c?.d, y = something(x) {</font></div><div><font face="monospace, monospace"> …</font></div><div><font face="monospace, monospace">}</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">would then have to become this:</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="monospace, monospace">let x = a?.b.c?.d<br></font></div><div><div><font face="monospace, monospace">if x != nil {</font></div><div><font face="monospace, monospace"> let y = something(x)</font></div><div><font face="monospace, monospace"> if y != nil {</font></div><div><font face="monospace, monospace"> …</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">}</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">I'm fine with</font></div><div><font face="arial, helvetica, sans-serif"> </font><font face="monospace, monospace">if let x</font></div><div><font face="arial, helvetica, sans-serif">as a shortcut for</font></div><div><font face="arial, helvetica, sans-serif"> </font><font face="monospace, monospace">if let x = x</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">It just reads a bit weird - like declaring an immutable variable with no type and no value.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 5:19 PM, Jeff Kelley via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I’ve had similar ideas to this. Instead of ditching the <font face="Menlo">if let</font> syntax altogether, another approach would be to use the existing name if no new name is given, so that this code:<div><br></div><div><font face="Menlo"><span style="white-space:pre-wrap">        </span>if let foo = foo { /* use foo */ }</font></div><div><br></div><div>could become this code:</div><div><br></div><div><font face="Menlo"><span style="white-space:pre-wrap">        </span>if let foo { /* use foo */ }</font></div><div><br></div><div>In both cases, <font face="Menlo">foo</font> is non-optional inside the braces. If you gave it another name with the <font face="Menlo">if let</font> syntax, that would work as it does today.<br><div><br></div><div><br><div>
<div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><span style="border-collapse:separate;color:rgb(0,0,0);font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;border-spacing:0px"><div style="word-wrap:break-word"><div>Jeff Kelley</div><div><br></div><div><a href="mailto:SlaunchaMan@gmail.com" target="_blank">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" target="_blank">@SlaunchaMan</a> | <a href="http://jeffkelley.org" target="_blank">jeffkelley.org</a></div></div></span></div></div></div>
</div>
<br><div><blockquote type="cite"><div><div class="h5"><div>On Dec 11, 2015, at 11:11 AM, Daniel Hooper via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr"><div>A very common pattern in swift code is to "guard let" or "if let" optionals - this works by creating a new non-optional variable to be used by future code. Often, the new variable is given the same name as the original optional variable, creating a shadow variable. This approach leads to odd looking code like this:</div><div><br></div><div>if let nearestX = nearestX { closest = nearestX }<br></div><div>guard let layer = layer else { continue } </div><div>// use layer</div><div><br></div><div>At a glance, and to Swift newcomers, this code looks completely non-sensical. It's also verbose for simply ensuring the variable is non-nil. </div><div><br></div><div>The solution:</div><div>Swift should generate unwrapped shadow variables after nil checking. The compiler would treat the below code as if it had created an unwrapped shadow variable.</div><div><br></div><div><span><div>if nearestX != nil { closest = nearestX } // notice that nearestX isn't force unwrapped<br></div><div>guard layer != nil else { continue } </div><div>// use layer, without force unwrapping</div></span></div><div><br></div><div>Why force unwrapping isn't a good alternative: </div><div>You might suggest force unwrapping variables when they're inside an if or after a guard that checks for nil. While this does allow for the "layer = nil" syntax, it results in code that is less resilient to change. Imagine that this code was written:</div><div><br></div><div>{code:java}</div><div>if layer != nil {</div><div>// lots of code before //</div><div>layer!.backgroundColor = newColor</div><div>// lots of code after //</div><div>}</div><div>{code}</div><div><br></div><div>And much later, you need to use some of the the code in the if body elsewhere, so you copy and paste a huge chunk of it. You likely won't notice the force unwrap, and unless you're lucky, you probably didn't paste it into an if that checked layer for nil. So you get a crash. Because of this, it's important we make safe optional unwrapping as easy and sensical as possible, and minimize the situations that you would need to force unwrap.</div></div>
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=oWipMi1-2F8XMs8QRRfapD5Mv-2FGx-2FUhuBfYnkgklXk1OEowkvcvokU9-2BQ-2FinepiHdcAYDoRu-2B2bKxhoJcA53JwRaK-2FH0ZWPNf1osQqJzbUYy-2F4ZJEXS9qRKLIhCP7XqwLmVf52wSBGeejQ91JneYj6teGXKFoTyhxPsn5E68AdPD4LGXT4UuiGSyX5rBrKJvZWC-2FsSrG-2Ff1Bc80eq-2BEkHZ2KUKKEYOPebovbowpX35e2k-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=6ZGE61OxINd5lLe2xYh9Ku-2BXbixWNr2nvfzp2IB1sZiSTQIYRRpg1f4BDoavrWICfHX-2Bcl6VVVU0t57dFlvnanUnMbSsB1ek6lci8HAL4-2F22PYOgq5OSsbf-2FU4jHUCpL-2BKJIJWfkls5WZd3BJ1p-2BbjRgZaBeai5W94UWcyPvvX2W-2B15Uot4RYuQp3Eu-2BqYGRMhY0eHXXsLHfKMEo3RunfEjjebkIaSLGTvuwsqurgq8-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>