-1
So far I've agreed with none of the +1 votes I've read, and for the most part, none of the -1 arguments either as they merely prefer a slightly modified spelling. I tried to argue my opinion during the discussion thread, but maybe I didn't make my case very well. I'll try to do better here in my dissent.
let a = b
literally says "let a equal b", consistent with colloquial (though "math-y") language.
var a = b
is not a completely analogous verbiage, but uses a term of art in saying "variable a equals b", similar enough to "let variable a equal b". It has a similar appearance to the "let" statement visually, and is familiar to users of other programming languages.
if let a = b
says something like "if (i can) let a equal b ...". The colloquial meaning of "if" imply a condition so the abbreviation does not detract, especially to anyone familiar with other programming languages. But what "can equal" means needs to be learned unfortunately, and it's not immediately obvious that it relates to optionals. So an initial learning curve comes into play for the second unstated part: "if (i can) let a equal (unwrapped) b".
That part being unstated is a rough edge of the language, a = b
in an "if" statement does not mean the same as a = b
in a "let" statement, but it's clearly another context and so there seems to be little cognitive dissonance in practice.
Once this is learned from other "if let" statements, and mentally inserting the same unstated terms, if let a = a
says the obvious: "if (i can) let a equal (unwrapped) a ...".
These parts of Swift syntax are not an arbitrary salad of symbols. These Swift statements are the language of English and mathematics (sorry foreign language speakers). Function calls, for example, are more or less entirely mathematical, but quite literally so, and like all other C-based languages. Where Swift differs from other C-based languages it mostly improves clarity and readability. There is a relatively large set of keywords used sensibly, and this helps to make Swift feel clean and modern.
The problem with if let a
is that it's absolutely symbol salad, it omits entirely half of the statement that is meant: "if (i can) let a (equal itself)". But it doesn't only detract from the legibility of these "if" statement, it undermines every other use of "let": So is just let a
valid Swift too? Can I trust what "let" and "var" mean?
I wish other parts of Swift syntax were evolving more in the direction away from symbol salad, pattern matching with "case" for example. Uses in switch statements they are comprehensible, but while if case let
shared a consistency with them, as statements they say nothing. Furthermore some things in Swift that you'd want to mean "if x is y" must be written oppositely, like if case y = x
. This is cognitive dissonance that undermines the meaning of "case" everywhere: hesitancy and uncertainly when you read it, hesitancy and uncertainly when you write it, and I claim if let a
is similar.
Other complaints against this proposal prefer something like if let a?
, but while these alternatives might arguably have a greater consistency (I haven't followed the arguments closely), unfortunately they are also symbol salad. It doesn't look like anything to me.
I know there are drawbacks to the status quo, but there should be a way to solve this in a way that maintains clarity. In the discussion I tried to endorse the addition of the "unwrap" keyword, and its potential to increase readability in this use case and possibly others. Maybe this will resonate with others who can echo my dissent and re-open discussion. There's an argument to be made against adding more keywords carelessly, but I don't think conserving keywords at great cost to comprehension is in the spirit of Swift.
I don't wish for Swift evolve towards more cognitive dissonance, but rather less. It looks, however, like this proposal is getting more support than not. If the community really wants to allow the shortcut if let a
syntax (after all, many other shortcuts exist already) I guess I can only hope that it doesn't become a recommended idiom with warnings and fix-it's demanding its use. If there's a growing resonance for the "unwrap" keyword, maybe it can come in a separate pitch in addition to this shortcut, not instead of it.
-
Is the problem being addressed significant enough to warrant a change to Swift?: yes
-
Does this proposal fit well with the feel and direction of Swift?: some of the worst parts of Swift maybe, not the best parts of Swift, and I hope the direction of the language isn't to be more like this proposal
-
If you have used other languages or libraries with a similar feature...: no
-
How much effort did you put into your review? A glance, a quick reading, or an in-depth study?: a full reading of the proposal, not a deep study of all the arguments during discussion, and my reaction was somewhat of a knee-jerk one but I've tried to reason through and articulate my immediate apprehension