(Is this the right place to post? 2 hours later and no comments?)
I appreciated the discussion of the alternate syntax of ->
for switch expressions (0380-alternative-syntax)
case n -> m
was rejected as unhelpful to learn for switch
, and as presenting numerous problems for if
.
I think it would be very helpful to use ->
for switch expressions (and not for if). There's no precedent or even reason to use ->
for if, and wanting to combine the two syntaxes may be harmful.
A distinct switch syntax is actually easier to learn and reflects the new usage. More importantly, the ->
form makes it much easier to read and understand switch expressions. And developers can/should prefer switch expressions to if or to switch statements because the semantic invariants make code easier to read and reason about.
It's tempting to conflate switch
and if
because switch
is gaining so much power, and you can replace switch
with if
. But switch
is intentionally a simpler, more constrained assertion about cases, and switch expressions would constrain them even further to a single result of a single type. It's these restrictions that make developers prefer switch
to if
: they're easier to reason about than if
and offer more guarantees. The switch expression in particular has the nice allure of something like f(x) -> y
, single input and output.
It's a good idea to treat switch expressions differently than if expressions or switch statements. Switch
is already unique in creating an implicit scope context without {}
(unique aside from the top-level scope?). Swift differs here from other languages which require break and use a common namespace.
Using ->
means the reader can see only the switch case and know immediately that it is an expression. It confuses developers when they need to know the outer context to understand the local code. Swift forums everywhere are littered with questions about code "not working" inside implicit view builders.
That's made worse in this case when switches are used as implicit returns; without ->
, there will be NO indication in the text why the compiler is complaining, when the code "works fine" elsewhere. Indeed, the confusion will only increase if, or when, multiple expressions are permitted in the case scope.
With ->
, both the reader and the learner know to apply any special restrictions (listed in this proposal). But even if the restrictions are eventually lifted, the user should really consider the expression form differently than the switch form, as a functional result instead of a procedural list of operations.
Although any switch can be converted to an if statement, it really means something else: that there are (only) these enumerated cases to consider. While the "case" has evolved from a single int to permit binding and pattern matching, but it still is mostly restricted to something like staticly-determinable code "matching" (in some way) as single value type. (Until you venture into the where clause...).
Switch
vs if
is a bit like the difference between truth tables and boolean logic: they are logically interchangable, but we consider tables via simpler discrete/pigeon-hole principles, while with if expression logic we have to consider unbounded evaluation. I believe code is usually clearer when it uses mechanisms of lesser power that say more about the underlying logic. Replacing if with switch usually makes things clearer, and it will be even more so for the more-restricted form of switch expressions. While where clauses and multiple expressions muddy this distinction, code not using those forms should read as cleanly as f(x) -> y
Indeed, while this proposal applies to both if
and switch
, I'd bet it results in a bigger increase in the use of switch, and that the vast majority are simple f(x)->y without side effects or where
delegation. It's the restricted form of expressions that's most helpful.
But higher prevelance means even if you believe the chance of confusion is small, it still creates a huge drag on developer time, forum traffic, and perceptions of Swift. Using ->
signals immediately that this is the new form of switch, introduced with this proposal, and people can search and learn and read and write accordingly.
(Sorry for the length/duplication; and adding code samples didn't really help with clarity.)