What is your evaluation of the proposal?
+1.
Inherently if let x = x
is not in my view inherently clearer to a new Swift developer than if let x
. As an Objective-C developer learning Swift, encountering if let x = x
was initially counter-intuitive since it appears to be assigning a variable to itself. So whichever syntax is used, there is a learning curve to understand it relates to unwrapping an optional, and once learned the shorter version is clearer to read and write.
I am strongly against the alternative proposal of if let x?
. In other contexts, a ? attached to a variable name conveys that the variable is optional, such as in if let valueOfInterest = optionalObject?.variable
. Extending this, if let x?
is essentially arguing to omit the left side of the current syntax:
if let x?
reads to me as if let (x =) x?
Rather than the proposal, which in my view much more logically proposes to omit the right side of the current syntax:
if let x
reads to me as if let x (= x?)
The impression that the ? syntax gives to me, as a naive reader, would be that I might still be dealing with an optional, not that I have now unwrapped one.
I think the main potential downside to the proposal is that if let x = x
is counter-intuitive to the point that it forces a new Swift programmer to learn about optionals and about variable shadowing, whereas it might be possible to think from if let x
that you are simply unwrapping the current variable, rather than shadowing it. However, any attempt to use x out of the scope of the closure would immediately give compiler warnings that x was optional, alerting the developer to an error in their understanding. So I don't think this argument against holds much weight on inspection.
Personally, I will particularly enjoy being able to guard let foo else { return }
.
Is the problem being addressed significant enough to warrant a change to Swift?
Yes. Clearer code, removing a small amount of unnecessary duplication.
Does this proposal fit well with the feel and direction of Swift?
Absolutely.
In contrast to some other respondents, I feel it reads very clearly: if (you will) let (me have) x { ā¦ }
If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
I havenāt used such a language.
How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
I read the proposal, every reply above, and considered for a bit what I thought as a regular old person writing Swift each day.