<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Apologies for the empty message, somehow managed to send by accident.</div><div class=""><br class=""></div><div class="">Anyway, I wanted to mention that I’m uncertain about this proposal, as I’ve actually found myself using trailing closures less and less due to their somewhat ambiguous syntax (visually I mean), and when I do use them I do the following:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo = myArray.map(){ $0 + 1 }</div><div class=""><br class=""></div><div class="">i.e- to clarify that it’s a method and not a property.</div><div class=""><br class=""></div><div class="">The only real advantage to them is not having to include the label, but that’s something we can omit anyway, and the parenthesis aren’t exactly burdensome to include around the closure. So I’m actually starting to lose my initial enthusiasm for trailing closures I think, as writing myArray.map({$0 + 1}) isn’t exactly costing me much, but is more explicit about what’s going on. I meant even be pro removal of trailing closures, not sure yet.</div><br class=""><div><blockquote type="cite" class=""><div class="">On 23 Mar 2016, at 12:27, Ilya Belenkiy via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="white-space:pre-wrap" class="">+1, a very useful change. I'd use it in lots of places in my code.</div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Mar 23, 2016 at 2:03 AM Chris Lattner via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Hi everyone,</div><div class=""><br class=""></div><div class="">This is a proposal for a very narrow extension to the guard statement. I consider it to be a bug fix, but since it is a language extension, I feel that it should go through the evolution process. Thoughts appreciated!</div><div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div><div class=""><b class=""><br class=""></b></div><div class=""><b class="">Introduction & Motivation<br class=""></b><br class="">The three statements `if`, `while`, and `guard` form a family that all take a rich form of conditions that can include one or more boolean conditions, `#available` clauses, and `let`/`case` pattern bindings. These are described by the `condition-clause` production in the TSPL reference section and as a `stmt-condition` in the compiler source code.</div><div class=""><br class=""></div><div class="">Today, these do not permit trailing closures in any top-level expressions embedded in the condition, because that would be generally ambiguous with the body of an if or while statement:</div><div class=""><br class=""></div><div class=""><span style="white-space:pre-wrap" class="">        </span>if foo { // start of trailing closure, or start of the if body?</div><div class=""><br class=""></div><div class="">While it would be possible to tell what is intended in some cases by performing arbitrary lookahead or by performing type checking while parsing, these approaches have significant consequences for the architecture for the compiler. As such, we’ve opted keep the parser simple and disallow this. Unrelated to this proposal, I landed a patch (<a href="https://github.com/apple/swift/commit/30ec0f4128525a16f998e04ae8b1f70180627446" target="_blank" class="">https://github.com/apple/swift/commit/30ec0f4128525a16f998e04ae8b1f70180627446</a>) which *greatly* improves the error messages in some of the most common cases where a developer accidentally tries to do this. </div><div class=""><br class=""></div><div class="">However, while this approach makes sense for `if` and `while` statements, it does not make sense for ‘guard': The body of a guard statement is delineated by the `else` keyword, so there is no ambiguity. A brace is always the start of a trailing closure.</div><div class=""><br class=""></div><div class="">From a historical perspective, the current situation was an oversight. An earlier design for `guard` did not include the `else` keyword (it used the `unless` keyword), and I forgot to fix this when we decided to resyntax it to `guard/else`. </div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">Proposed solution</b><br class=""><br class="">The solution is simple: allow trailing closures in guard bodies. This would allow this silly example to compile correctly:</div><div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">func</span><span class=""> f(arr : [</span><span style="color:#703daa" class="">Int</span><span class="">]?) {</span></div></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""> </span><span style="color:#bb2ca2" class="">guard</span><span class=""> </span><span style="color:#bb2ca2" class="">let</span><span class=""> x = arr?.map {$0+</span><span style="color:#272ad8" class="">1</span><span class="">} </span><span style="color:#bb2ca2" class="">else</span><span class=""> {</span></div></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""> preconditionFailure()</span></div></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""> }</span></div></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""></div></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""> </span><span style="color:rgb(0,132,0)" class="">// ...</span></div></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div></div></blockquote><div class=""><span class=""></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><b style="font-family:Helvetica;font-size:12px" class="">Detailed Design</b><br style="font-family:Helvetica;font-size:12px" class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""></div>The impact on the compiler is trivial, here’s a patch:</div><div class=""><br class=""></div><div class=""></div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""><br class=""><br class=""><b class="">Impact on existing code<br class=""></b><br class="">There is no impact on existing code. This only makes formerly invalid code start being accepted.</div><div class=""><br class=""></div><div class=""><br class=""><b class="">Alternatives considered<br class=""></b><br class="">There are three primary alternatives: do nothing, expand the scope of ‘if' and ‘while’ conditions as well, and significantly change the syntax of guard.</div><div class=""><br class=""></div><div class=""><b class="">Do nothing</b>: It can be argued that this change would make guard inconsistent with the restrictions of ‘if’ and ‘while’ and that inconsistency would be confusing. On the other hand, I am arguing that this is an arbitrary restriction.</div><div class=""><br class=""></div><div class=""><b class="">Expand the scope of “if” and “while” statements:</b> Through enough heroics and lookahead we could consider relaxing the trailing closure requirements on `if` and `while` statements as well. While this could be interesting, it raises several ambiguity questions. For example, we need significant lookahead to realize that “a” here is not a trailing closure, since we have a closure expression being fully applied after it:</div><div class=""><br class=""></div><div class=""><span style="white-space:pre-wrap" class="">        </span>if foo { …a... } { …b… }()</div><div class=""><br class=""></div><div class="">this could be addressed with whitespace rules or other approaches, but since any such direction would be compatible with this proposal, I see it as a separable potential extension on top of this basic proposal.</div><div class=""><br class=""></div><div class=""><b class="">Change the syntax of guard: </b>I only list this for completeness, but we could eliminate the `else` keyword, making guard more similar to `if` and `while`. I personally think that this is a really bad idea though: the guard statement is not a general `unless` statement, and its current syntax was very very carefully evaluated, iterated on, discussed, and re-evaluated in the Swift 2 timeframe. I feel that it has stood the test of time well since then.</div><div class=""><br class=""></div><div class=""><br class=""></div></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>