SE-0345: `if let` shorthand for shadowing an existing optional variable

Thank you for putting this together @cal . When I first learned Swift I found the if let syntax confusing. I felt like the language was pushing me towards creating a new variable name.

if let myNonOptionalVariable = myVariable {
  doSomething(myNonOptionalVariable)
}

I was surprised to learn that the idiomatic way to write this code (at least in my circle) was to repeat the variable name.

if let myVariable = myVariable {
  doSomething(myVariable)
}

At the same time, repeating the same identifier felt off in its own way. I had trouble believing that this was how I was supposed to use the language.

This proposed language change is trivial to grok if you are a seasoned Swift programmer, would improve code readability in large real-world projects, and IMHO makes the language more approachable and self-documenting for new Swift programmers.

7 Likes