<div dir="ltr">As Jacob has just touched on, this suggestion (in various forms) has been proposed before and is proposed often. However, syntactic sugar is out of scope for this phase of Swift 4 evolution.<div class="gmail_extra"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Aug 28, 2016 at 2:54 PM, Nicholas Exner via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><span></span></div><div>Dear Swift Evolution Group Members,<div>         Recently, as I was reviewing some code that I wrote, I was wondering what the community’s thoughts are on the possible value of having a special “if let” self-assignment operator added to the language (such as =? ).  A sort of syntactic sugar that allows for optional unwrapping into a scoped variable of the same name as the variable in the larger scope.  Here is some before and after code of what I am contemplating:</div><div><br></div><div><b>Current:</b></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span><span> str:</span><span style="color:#703daa">String</span><span>?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#4f8187">str</span><span style="color:#000000"> = </span><span>&quot;Hello&quot;</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">if</span><span> </span><span style="color:#bb2ca2">let</span><span> str = </span><span style="color:#4f8187">str</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#3d1d81">print</span><span>(str)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><b>Abbreviated:</b></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><div style="margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">var</span><span> str:</span><span style="color:rgb(112,61,170)">String</span><span>?</span></div><div style="margin:0px;line-height:normal;color:rgb(209,47,27)"><span style="color:rgb(79,129,135)">str</span><span style="color:rgb(0,0,0)"> = </span><span>“Hello”   // Output is Hello, not Optional(“Hello”)</span></div><div style="margin:0px;line-height:normal;min-height:13px"><span></span><br></div><div style="margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">if</span><span> </span><span style="color:rgb(187,44,162)">let</span><span> str =?</span><span> {</span></div><div style="margin:0px;line-height:normal"><span>    </span><span style="color:rgb(61,29,129)">print</span><span>(str) </span><span style="color:rgb(209,47,27)">// Output is also Hello, not Optional(“Hello”)</span></div><div style="margin:0px;line-height:normal"><span>}</span></div></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;line-height:normal">The value that I perceive of adding this to the language would be to allow a short-hand way to unwrap in a “if let&quot; conditional and allowing developers to reduce duplication when a unique variable name is not needed.  By the way, I’m a big fan of ternary operators which the community already sees of value.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Let me know your thoughts.  Is this something that could be put into a proposal?</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Cheers,</div><div style="margin:0px;line-height:normal">—Nick—</div><div style="margin:0px;line-height:normal">.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></div><div><br></div></div></div><br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>