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

I found this to be a very interesting point. Reading through those who have said they found the proposed syntax lacks clarity, I don't remember seeing any examples of what the proposed shorthand would be confused with.

I think the notion of an unwrap keyword, or requiring a sigil like ?, stems from the fact that the existing if let foo = bar or the shadowing version if let foo = foo is not immediately clear for a newcomer as there is no indication in the syntax that unwrapping is occurring. So the existing optional binding syntax could very easily be said to be unclear when first encountered.

That said, the if let foo = bar syntax has been in place since the initial introduction of Swift and the idiom is taught to new Swift developers along with optionals. (I still remember watching the initial WWDC session that walked through the basics of the language and thinking, "oh I guess that's how you do that" when if let foo = bar was introduced.)

Despite the syntax having no indication that unwrapping is occurring, it is widely used and understood. So, I think the preference for a new keyword or a sigil stems from lack of obvious meaning of the existing optional binding syntax when encountered for the first time, although in practice the syntax is used and understood without confusion. I think the same thing will be true with the proposed shorthand.

Going through an introductory Swift book or course, typically the first time you ever see if let foo = bar syntax, you are told exactly what it means. And I would imagine the same would be true for if let foo.

I am curious, for those who feel it is unclear, knowing how if let foo = foo works, what other things would you think if let foo does? And once you learn what if let foo does, what other things would you continually confuse it with?

12 Likes