<div dir="ltr"><div>I don't know if this will help clarify things, so take or leave as you like:</div><div><br></div><div>I wasn't aware before this discussion that the first of the comma-separated expressions of an if let could be just an 'if'.<br></div><div><br></div><div>Now I have learned it, it just seems to be an odd construction: the first clause of an if can be a boolean expression, a let binding, a var binding or a case let/var binding, and then after the first comma every boolean expression must be preceded by 'where'. There's no obligation for the boolean expression to relate to the constant or variable just bound.</div><div><br></div><div>My original premise was simply to allow boolean expressions and binding expressions to alternate within the if; boolean operators like & and | would separate boolean expressions, commas would separate binding expressions, and semi-colons would allow alternating from one to the other. I hadn't anticipated that this would deprecate any existing code; a let followed by a var in the if would still be comma-separated binding expressions - but I'm not objecting to deprecating something if it makes the code clearer.</div><div><br></div><div>So (and I apologise for the trivialised example) my proposed change would be that this:</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">if</span> a == <span style="color:rgb(39,42,216)">5</span>, <span style="color:rgb(187,44,162)">let</span> b = c <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)">5</span>, <span style="color:rgb(187,44,162)">let</span> d = c, <span style="color:rgb(187,44,162)">var</span> e = array[<span style="color:rgb(39,42,216)">0</span>], f = array[<span style="color:rgb(39,42,216)">1</span>] <span style="color:rgb(187,44,162)">where</span> e == f, <span style="color:rgb(187,44,162)">case</span> .Some(<span style="color:rgb(187,44,162)">let</span> g) = c <span style="color:rgb(187,44,162)">where</span> g.<span style="color:rgb(112,61,170)">characters</span>.<span style="color:rgb(112,61,170)">count</span> > <span style="color:rgb(39,42,216)">2</span> { }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:arial,sans-serif;font-size:small">would become</span><br></p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">if</span> a == <span style="color:rgb(39,42,216)">5</span>; <span style="color:rgb(187,44,162)">let</span> b = c; array.<span style="color:rgb(112,61,170)">count</span> > <span style="color:rgb(39,42,216)">5</span>; <span style="color:rgb(187,44,162)">let</span> d = c, <span style="color:rgb(187,44,162)">var</span> e = array[<span style="color:rgb(39,42,216)">0</span>], f = array[<span style="color:rgb(39,42,216)">1</span>] <span style="color:rgb(187,44,162)">where</span> e == f; <span style="color:rgb(187,44,162)">case</span> .Some(<span style="color:rgb(187,44,162)">let</span> g) = c <span style="color:rgb(187,44,162)">where</span> g.characters.count > <span style="color:rgb(39,42,216)">2 </span>{ }<br></p></div><div><br></div><div>So, mostly the same, but 'where' can be used only where it clarifies to the reader that it's qualifying a binding, rather than being used because it's the only way to follow a binding expression with a boolean expression.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 5, 2016 at 12:04 AM, James Hillhouse IV 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">Here’s a question that Drew raised as he was educating me on this discussion. Given that the following works, are you proposing to deprecate,<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> a = <span style="color:#272ad8">2</span>, <span style="color:#bb2ca2">var</span> b = <span style="color:#272ad8">3</span>, <span style="color:#bb2ca2">let</span> c = <span style="color:#272ad8">4</span>, <span style="color:#bb2ca2">var</span> d = <span style="color:#272ad8">5</span>{ } <span style="color:#008400">// 1</span></div></div><div><br></div><div>in favor of?</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> a = <span style="color:#272ad8">2;</span> <span style="color:#bb2ca2">var</span> b = <span style="color:#272ad8">3;</span> <span style="color:#bb2ca2">let</span> c = <span style="color:#272ad8">4;</span> <span style="color:#bb2ca2">var</span> d = <span style="color:#272ad8">5</span>{ } <span style="color:#008400">// 2</span></div></div><div><br></div><div>If semi-colon is still being considered, can Joe or Jordon clarify?</div><span class=""><div><br><div><blockquote type="cite"><div>On Feb 2, 2016, at 7:40 PM, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div><br>On Feb 2, 2016, at 5:13 PM, Jordan Rose via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Feb 2, 2016, at 16:53, Ross O'Brien via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">For example: getting the first element from an array of optionals. Here's my sample trivial example:</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:rgb(187,44,162)">var</span><span> </span>array : [<span style="color:rgb(112,61,170)">Int</span>?] = []</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:rgb(187,44,162)">if</span><span> </span><span style="color:rgb(187,44,162)">let</span><span> </span>x = array[<span style="color:rgb(39,42,216)">0</span>]<span> </span><span style="color:rgb(187,44,162)">where</span><span> </span>array.<span style="color:rgb(112,61,170)">count</span><span> </span>><span> </span><span style="color:rgb(39,42,216)">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">                </span><span style="color:rgb(61,29,129)">print</span>(x)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>}</div></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">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></blockquote><br></div><div>We already allow this, with commas:</div><br><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>var array: [Int?] = []</div><div>if !array.isEmpty, let x = array[0] {</div><div> print(x)</div><div>}</div></blockquote></div></div></blockquote></div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">That's true, but there are some problematic ambiguities with our current syntax, since 'let' and 'case' conditions both admit multiple sub-conditions also separated by commas, e.g. 'if let x = y, y = z { }' or 'if case let x = y, let y = z'. Using ';' for this purpose would be less ambiguous.</div></div></blockquote></div><br></div></span></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>