Not to stretch the analogy to absurdity, but yes: there isn't a need to redo the entire road, if you're okay with a road full of patches that will inevitably break up and become even bigger potholes given a year or two of frost and thawing.
The future directions section gives us a glimpse of what that patchy road might look like some years down the line, and I can't say I really like it. Let's have a look:
if let foo as? Bar { ... }
That looks alright enough, but the inconsistency with case patterns is growing wider. For the simple unwrapping, cases need a question mark (if case let x? = x { ... }
), the proposed syntax doesn't. For the future direction, we need to use a question mark where cases need none (if case let foo as Bar = foo { ... }
). No way to explain that besides historical baggage (<=> patched potholes).
And that's only the first future direction. Let's look at the next one:
if ref foo { ... }
I brought this up in the pitch thread, but I don't think the proposed syntax translates to rarer alternatives to let
/var
at all. I can't imagine someone without pretty extensive language knowledge could intuitively understand that foo
is being unwrapped and shadowed here. It reads more like querying some information about foo ("is foo a reference?", "is foo referenced somewhere else?").
Next one:
// `mother.father.sister` is optional
if ref mother.father.sister {
// `mother.father.sister` is non-optional and immutable
}
if inout &mother.father.sister {
// `mother.father.sister` is non-optional and mutable
}
This is the one I like the least, because you not only need to understand that shadowing and unwrapping is happening by a single niche keyword, now we're changing types and behaviour multiple levels of nesting deep into other types. Imo this just breaks the mental model completely. I can no longer be sure that if I read something.other.specificPropertyOfTypeA
that it's actually of type A and behaves in any way similar to other places I read x.specificPropertyOfTypeA
.
I know this review is about the proposed feature and not about the future directions, but I believe the future directions illustrate nicely how this is a syntax that doesn't really generalize/compose and thus imo doesn't really fit into Swift. I actually think that just the proposed feature on it's own isn't very confusing and would probably do the job it's designed for adequately, but the future directions make me a strong -1.