<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">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="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span><span style="font-variant-ligatures: no-common-ligatures"> str:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">String</span><span style="font-variant-ligatures: no-common-ligatures">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">str</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> = </span><span style="font-variant-ligatures: no-common-ligatures">"Hello"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"></span><br></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">let</span><span style="font-variant-ligatures: no-common-ligatures"> str = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">str</span><span style="font-variant-ligatures: no-common-ligatures"> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">print</span><span style="font-variant-ligatures: no-common-ligatures">(str)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">}</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"><br></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"><b>Abbreviated:</b></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"><div style="margin: 0px; line-height: normal;"><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span style="font-variant-ligatures: no-common-ligatures;"> str:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span style="font-variant-ligatures: no-common-ligatures;">?</span></div><div style="margin: 0px; line-height: normal; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">str</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> = </span><span style="font-variant-ligatures: no-common-ligatures;">“Hello” // Output is Hello, not Optional(“Hello”)</span></div><div style="margin: 0px; line-height: normal; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures;"></span><br></div><div style="margin: 0px; line-height: normal;"><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">if</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span style="font-variant-ligatures: no-common-ligatures;"> str =?</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div><div style="margin: 0px; line-height: normal;"><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);">print</span><span style="font-variant-ligatures: no-common-ligatures;">(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 style="font-variant-ligatures: no-common-ligatures;">}</span></div></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"><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" 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 style="font-variant-ligatures: no-common-ligatures"><br></span></div><div><br></div></div></body></html>