It appears that there is some concern that I'm trying to side track the discussion with "forward-looking criticisms" relating to ownership. Perhaps the problem is that I'm not be explicit enough about my concern, so here's an attempt to fix that.
Part of my concern with the framing of this entire discussion is that it is of the form: "if let x = x {
is obviously redundant, happens all the time, and there is an obvious sugar: obviously we should just do it".
I can see the apparently simple truth to those statements, and I've raised a concern about var
, but let me raise a much bigger issue: ignoring syntax, the current if let
semantics are the wrong thing to sugar. The only reason this is common is because Swift doesn't have the "right thing" yet. In my opinion, it would be much better to sugar the right thing rather than ensconcing the mistake of the past.
What on earth could I be talking about?
The problem is that if let
makes a semantic copy of the optional payload. In some cases, the ARC optimizer can eliminate the additional reference counts etc, but it would be far more efficient to sugar the operation of "rebinding a name as a borrow of the optional payload" since this will be more efficient in general, and will work correctly with move-only payloads.
This is also important because the entire concept of if let x = x
is about simple identifier expressions, not unwrapping arbitrary expressions like x.y.z
.
If this is the better model that we want people to steer people towards, then I think it would be very destructive to sugar the copy syntax right before introducing the "right" thing. It would miss the opportunity to steer future swift code, and lead people to writing less efficient code in practice.
I think it is important to determine the right semantics (e.g. answering whether a borrow-model would be better or not) before tackling syntax. If a borrow model is better, then making the syntax be if let x {
to form a borrow would clearly be wrong. I also don't see how something like if ref x {
would be better than if unwrap x {
etc.
That said, my meta point is that we should be starting from the semantic design and working towards syntax.
-Chris