[swift-evolution] [proposal] Allow trailing closures in 'guard' conditions

Thorsten Seitz tseitz42 at icloud.com
Wed Mar 23 12:20:39 CDT 2016


+1
Some more comments inline.

Am 23.03.2016 um 10:52 schrieb Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org>:

>> The solution is simple: allow trailing closures in guard bodies.  This would allow this silly example to compile correctly:
>> 
>> func f(arr : [Int]?) {
>>  guard let x = arr?.map {$0+1} else {
>>    preconditionFailure()
>>  }
>> 
>>  // ...
>> }
> 
> This sounds perfectly reasonable.
> 
>> Alternatives considered
>> 
>> There are three primary alternatives: do nothing, expand the scope of ‘if' and ‘while’ conditions as well, and significantly change the syntax of guard.
> 
> A fourth alternative would be to change the syntaxes of `if` and `while` (and probably `for` and `switch`) to also have a keyword in this position.
> 
>    if expr then { code }
>    while expr do { code }
>    for elem in expr do { code }
>    switch expr among { code }

Switch does not need a new keyword, as it already has 'case'.
Adding 'then' to if statements would easily allow if-then-else expressions without braces by just replacing the blocks with expressions.

-Thorsten 

> 
> I'm not going to say I advocate for this option, but it *would* clearly mark the end of the condition so that trailing closures could be brought to all of these statements, so it seemed worth mentioning.
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution


More information about the swift-evolution mailing list