[swift-evolution] guard let x = x

David Waite david at alkaline-solutions.com
Thu Oct 27 19:52:57 CDT 2016


> On Oct 26, 2016, at 9:37 AM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:

> To me, this is the most promising direction, but I’d suggest the use of “unwrap" as the keyword.  If you compare these two:
> 
> a) guard let foobar = foobar else { … }
> b) guard unwrap foobar else { … }
> 
> I think that b) wins by virtue of eliminating repetition ("foobar = foobar" fails DRY principles), but retains clarity by introducing a word into the grammar that people already commonly know and use, and which is googlable if they don’t.
> 
> This also gives us the conceptual hook to make the “unwrapping an optional” behavior (which occurs with if let, optional chaining, etc) be something that could be extended to other similar user defined types, such as a Result type.

I know that there has been many conversations about a cleaner syntax for conditional unwrapping of optionals on this list. My concern in the past has been whether this makes it confusing for new developers to understand whether “foobar” is a shadowed variable within the block (or for guard, after the block) or if it is an alias.

-DW


More information about the swift-evolution mailing list