<div dir="ltr"><div>Since Swift almost abolished the semi-colon as an end of statement marker, and Swift 3 is abolishing is in C-style for-loops, I thought I'd suggest something else for it to do.</div><div><br></div><div>Swift 1.2 allowed developers to pack multiple if let bindings into one if let, using comma separators. This was well received as it removed the disliked 'pyramid of doom' - lots of nested indented if statements.</div><div><br></div><div>I'd like to propose using the semi-colon to pack if lets and regular ifs into one if. The format would essentially be:</div><div>if <boolean expression>; let x = x as? Foo; <boolean expression>; let y = x.<property>, z = y.<property> // and so on</div><div>{</div><div> // closure which only happens if both expressions are true and all three bindings happen; note that the last two are comma-separated as they're both 'if let's.</div><div>}</div><div><br></div><div>There would only be one actual 'if'. The semi-colon<br></div><div>This would also be applicable to guard and guard let. I've had a number of occasions where I've written multiple guard statements with identical else closures. I'd like to avoid some of that duplicate code.</div><div><br></div><div>I realise that it's already possible to combine conditions and conditional bindings in one if, using the 'where' keyword, but I don't think it's clear - certainly not as clear as when where filters for-in ranges or case statements.</div><div><br></div><div>For example: getting the first element from an array of optionals. Here's my sample trivial example:</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span><span style="color:rgb(187,44,162)">var</span> array : [<span style="color:rgb(112,61,170)">Int</span>?] = []</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span><span style="color:rgb(187,44,162)">if</span> <span style="color:rgb(187,44,162)">let</span> x = array[<span style="color:rgb(39,42,216)">0</span>] <span style="color:rgb(187,44,162)">where</span> array.<span style="color:rgb(112,61,170)">count</span> > <span style="color:rgb(39,42,216)">0</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">                </span><span style="color:rgb(61,29,129)">print</span>(x)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>}</p></div><div>This code doesn't work. The array index is out of range. But there's no way I know of to rearrange the 'if' to ensure the array isn't empty before binding to the first element - the developer has to write a nested if statement.</div><div><br></div><div>My suggested syntax would present the if like this:</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span><span style="color:rgb(187,44,162)">if</span> array.count > <span style="color:rgb(39,42,216)">0</span>;</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre">        </span><span style="white-space:pre">        </span><span style="color:rgb(187,44,162)">let</span> x = array[<span style="color:rgb(39,42,216)">0</span>]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">                </span>print(x)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>}</p></div><div><br></div><div>The semi-colon would read as 'and' in the same way the comma does in a multiple if let or && does in an if; it allows the developer to alternate between boolean expressions and conditional bindings. (The downside here is that three distinct punctuation symbols are all essentially used to mean the same thing.)</div><div><br></div><div>Is this worthy of discussion?</div><div><br></div></div>