[swift-evolution] An implicit return for guard
Dany St-Amant
dsa.mls at icloud.com
Mon Jun 20 19:29:13 CDT 2016
> Le 20 juin 2016 à 02:30, Yarden Eitan via swift-evolution <swift-evolution at swift.org> a écrit :
>
> ...
>
> My proposal is to allow for an implicit return when no ending statement is provided. We could take this one step further and have the compiler aware of it’s most inner scope and see if it is a while loop for example and implicitly allow a break. But I think at least as a first step, by having an implicit “return” we are saving the repetitiveness on many cases where there are multiple guard statements and the return from them is obvious.
A 'guard' in a loop can commonly be used with either 'continue' or 'break', the compiler would not be able to guess your intent. And whatever default would be selected, it would be the wrong one for many scenarios.
The 'return' is an important keyword which, I think, should not be obfuscated as one often need to quickly find all exit point of a function, when debugging/analyzing code.
> This goes along the line of the Swift “switch” statement, which doesn’t follow it’s predecessors and force a “break” but rather it is already implicitly there.
If you bring the consistency card, I would be more on the page of making 'break' mandatory and explicit in the 'switch'.
Dany
> If this proposal is too much of a leap, an alternative is to allow an implicit return but provide a warning (not an error). This warning can be suppressed using an @implicitreturn prefix to the guard statement or something along those lines (@implicitreturn guard a = b else { print(“foo”) } ).
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160620/c32c27f8/attachment.html>
More information about the swift-evolution
mailing list