[swift-evolution] guard let x = x
William Sumner
prestonsumner at me.com
Mon Oct 31 17:46:42 CDT 2016
> On Oct 31, 2016, at 3:44 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> 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
Your proposed form of unwrap naturally works in multiple places with clear readability:
var foo: Any?
if unwrap foo {}
while unwrap foo {}
guard unwrap foo else { return }
Preston
More information about the swift-evolution
mailing list