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

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


> On Mar 23, 2016, at 8:11 PM, Dany St-Amant via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 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.
> 
> Could such new keywords be optional, only required when the compiler see the construct as ambiguous? 

No.  If the compiler could tell when the keyword was necessary, we just wouldn’t require it! :-)

The other approach you could take is to only use this sort of keyword when a trailing closure is present, but at that point, you might as well just wrap the trailing closure with parentheses (which is already supported today, and the compiler now nudges you towards).

-Chris


More information about the swift-evolution mailing list