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

Chris Lattner clattner at apple.com
Wed Mar 23 23:49:40 CDT 2016


> On Mar 23, 2016, at 2:52 AM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> 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 }
> 
> 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.

Thanks, I’ll add this to the end of the proposal!

-Chris


More information about the swift-evolution mailing list