[swift-evolution] guard let x = x

Erica Sadun erica at ericasadun.com
Mon Oct 31 16:44:43 CDT 2016


> On Oct 31, 2016, at 1:51 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> 
> An alternative that would seem to satisfy some objections is to have a distinct "unwrap" statement--as in: "unwrap x else { ... }".

I'd be against this alternative as it is doing the work of a guard statement (including the "must exit scope" rule) with a new keyword and unneeded redundancy.

Adding "unwrap" is local. It performs one very common task with added safety (avoids unintended shadow effects) and introduces succinctness and clarity. In other words, it is a highly focused changed with a measurable benefit in use.

Compare:

* "Introduce the unwrap statement that acts like guard but is limited to optionals" creates unnecessary language verbosity.

* "guard x else ", meaning "treat optional values differently than all other items in a conditional list", fails because it obscures rather than adds intent. Worse, it would lead to issues with Boolean optionals whose wrapped value is later used within the same condition.

Xiaodi, you mentioned an alternative approach and I'd love a peek at what that is.

-- Erica




More information about the swift-evolution mailing list