<div dir="ltr"><div>I don&#39;t think we should discuss &quot;with&quot; here as that is a separate proposal. I&#39;m trying to draw attention of the approach of Swift generating shadow variables for you when an optional has been proven to be non-null. <br></div><div><br></div>I didn&#39;t say we should remove if/guard let. They would stay.<div><br></div><div>My proposal is to improve readability and reduce verbosity by generating shadow variables after &quot;x != nil&quot; checks. The &quot;with x {}&quot; and &quot;if let x {}&quot; approaches only reduce verbosity, at the additional cost of adding new keywords or syntax to the language, and do not really improve readability. Due to this, generating a shadow variable after &quot;x != nil&quot; is the most straightforward, lowest impact and readable approach.</div><div><br></div><div>ilya, you bring up a good point about &quot;create shadow-&gt;write to original-&gt;read shadow&quot; situations being potentially confusing. However &quot;if let x = x&quot; is very common in situations that this isn&#39;t an issue: taking a quick look at my code, 50% of all &quot;if/guard let x = x&quot; are on local variables that cannot possibly change out from under the shadow variable. If that could lead to confusion in a given situation, you could fall back on &quot;if let x = maybe_x&quot; to make it clearer that you&#39;re operating on a copy of a variable that won&#39;t change out from under you. Alternatively, the &quot;if x != nil { x.bla() }&quot; syntax could only be allowed on optionals in the current function scope.</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 11, 2015 at 11:41 AM Sean Heber &lt;<a href="mailto:sean@fifthace.com">sean@fifthace.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This has come up a few times. I’ve thrown my 2cents into a variant of “with” to solve this rather than messing with the meaning of if-let:<br>
<br>
For example:<br>
<br>
var x: MyType?<br>
<br>
with x {<br>
  x.someFunction()              // okay - this entire block is skipped if “x” is nil anyway<br>
}<br>
x.someFunction()                // not okay as-is since x is optional<br>
<br>
<br>
Using the definition  of “with” from Erica’s Method Cascading proposal <a href="https://gist.github.com/erica/6794d48d917e2084d6ed" rel="noreferrer" target="_blank">https://gist.github.com/erica/6794d48d917e2084d6ed</a>, you could even drop the variable reference itself from inside the block body:<br>
<br>
with x {<br>
  someFunction()                // would call “someFunction()” on “x”<br>
}<br>
<br>
<br>
And I’d additionally propose that if you wanted to replace if-let-else, you could potentially do this:<br>
<br>
if with x {<br>
  // x is known non-nil<br>
} else {<br>
  // x is known to be nil<br>
}<br>
// x is optional and nil-ness is unknown<br>
<br>
<br>
l8r<br>
Sean<br>
<br>
<br>
&gt; On Dec 11, 2015, at 10:28 AM, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; One problem with dropping if/guard let is something like this:<br>
&gt;<br>
&gt; if let x = a?.b.c?.d, y = something(x) {<br>
&gt;    …<br>
&gt; }<br>
&gt;<br>
&gt; would then have to become this:<br>
&gt;<br>
&gt; let x = a?.b.c?.d<br>
&gt; if x != nil {<br>
&gt;    let y = something(x)<br>
&gt;    if y != nil {<br>
&gt;        …<br>
&gt;    }<br>
&gt; }<br>
&gt;<br>
&gt; I&#39;m fine with<br>
&gt;    if let x<br>
&gt; as a shortcut for<br>
&gt;    if let x = x<br>
&gt;<br>
&gt; It just reads a bit weird - like declaring an immutable variable with no type and no value.<br>
&gt;<br>
&gt;<br>
&gt; On Fri, Dec 11, 2015 at 5:19 PM, Jeff Kelley via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt; I’ve had similar ideas to this. Instead of ditching the if let syntax altogether, another approach would be to use the existing name if no new name is given, so that this code:<br>
&gt;<br>
&gt;       if let foo = foo { /* use foo */ }<br>
&gt;<br>
&gt; could become this code:<br>
&gt;<br>
&gt;       if let foo { /* use foo */ }<br>
&gt;<br>
&gt; In both cases, foo is non-optional inside the braces. If you gave it another name with the if let syntax, that would work as it does today.<br>
&gt;<br>
&gt;<br>
&gt; Jeff Kelley<br>
&gt;<br>
&gt; <a href="mailto:SlaunchaMan@gmail.com" target="_blank">SlaunchaMan@gmail.com</a> | @SlaunchaMan | <a href="http://jeffkelley.org" rel="noreferrer" target="_blank">jeffkelley.org</a><br>
&gt;<br>
&gt;&gt; On Dec 11, 2015, at 11:11 AM, Daniel Hooper via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; A very common pattern in swift code is to &quot;guard let&quot; or &quot;if let&quot; 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:<br>
&gt;&gt;<br>
&gt;&gt; if let nearestX = nearestX { closest = nearestX }<br>
&gt;&gt; guard let layer = layer else { continue }<br>
&gt;&gt; // use layer<br>
&gt;&gt;<br>
&gt;&gt; At a glance, and to Swift newcomers, this code looks completely non-sensical. It&#39;s also verbose for simply ensuring the variable is non-nil.<br>
&gt;&gt;<br>
&gt;&gt; The solution:<br>
&gt;&gt; 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.<br>
&gt;&gt;<br>
&gt;&gt; if nearestX != nil { closest = nearestX } // notice that nearestX isn&#39;t force unwrapped<br>
&gt;&gt; guard layer != nil else { continue }<br>
&gt;&gt; // use layer, without force unwrapping<br>
&gt;&gt;<br>
&gt;&gt; Why force unwrapping isn&#39;t a good alternative:<br>
&gt;&gt; You might suggest force unwrapping variables when they&#39;re inside an if or after a guard that checks for nil. While this does allow for the &quot;layer = nil&quot; syntax, it results in code that is less resilient to change. Imagine that this code was written:<br>
&gt;&gt;<br>
&gt;&gt; {code:java}<br>
&gt;&gt; if layer != nil {<br>
&gt;&gt; // lots of code before //<br>
&gt;&gt; layer!.backgroundColor = newColor<br>
&gt;&gt; // lots of code after //<br>
&gt;&gt; }<br>
&gt;&gt; {code}<br>
&gt;&gt;<br>
&gt;&gt; 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&#39;t notice the force unwrap, and unless you&#39;re lucky, you probably didn&#39;t paste it into an if that checked layer for nil. So you get a crash. Because of this, it&#39;s important we make safe optional unwrapping as easy and sensical as possible, and minimize the situations that you would need to force unwrap.<br>
&gt;&gt;  _______________________________________________<br>
&gt;&gt; swift-evolution mailing list<br>
&gt;&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt;<br>
&gt;  _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt; <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>