If / guard shorthand for optional unwrapping

After writing many lines of:

if let foo = foo, bar = bar { .... }

and lines like:

guard let self = self else { ... }

it seems a little redundant to have to repeat the variable name twice when we want to simply unwrap the optional and use it by the same name.

What about this?

// If `foo` and `bar` are previously defined and reachable from this scope then this acts the same as `if let foo = foo, let bar = bar { ... }`
if let foo, let bar {

guard let self { ... }

If a new name should be assigned then that can also be done no different than it is now:

if myVar = foo { ... }

Just a thought, any feedback?

You might want to read this prior discussion of the same topic:


In addition to Topher's answer, you may take a look at this document for the if let foo, let bar part.

@toph42 @MrAlirezaa Thank you very much! When I went to look for prior discussions on the topic I missed that one.

1 Like
Terms of Service

Privacy Policy

Cookie Policy