Is "else"useless, with guard's {}?

All the examples I've seen with guard adopt the else keyword before the opening brace. If there isn't any other possiblity than else, then we don't need the keyword. (The IDE can make the "else-ness" clear, if it's not otherwise, without another keyword.) Is there another possiblity?

Accepted Reply

You're mostly correct. It would almost be syntactically unambiguous to drop the "else" keyword there. The principle reason to require it is for code clarity.


The secondary reason is the ambiguity: this happens when you use initializer values that use trailing closure syntax, because the compiler can't tell (without heroic analysis) whether the braces are a trailing closure or to "else" part of the guard.


-Chris

Replies

It reads better and removes ambiguity between a trailing closure argument to part of the guard clause and the else clause.

You're mostly correct. It would almost be syntactically unambiguous to drop the "else" keyword there. The principle reason to require it is for code clarity.


The secondary reason is the ambiguity: this happens when you use initializer values that use trailing closure syntax, because the compiler can't tell (without heroic analysis) whether the braces are a trailing closure or to "else" part of the guard.


-Chris