I want to address these points, because I think it is really important. They are both appeals to consistency: “We already use this spelling for a similar thing, so we should use it for this new thing.” There have been a few other comments along the same lines, not to mention the pitch itself. And I want to push back quite strongly:
Consistent use of a bad design, is bad.
To quote a member of the language workgroup:
In my view, the existing if case
syntax is bad. Really bad. It is incomprehensible to beginners, hard to remember for intermediates, and even experts I think will agree that it inverts the subject and object, thereby hampering code completion. It certainly does not make sense as an English sentence.
By contrast, matches
reads fluently.
I think the best way forward is to change the current proposal to use matches
, and either,
a. Extend this proposal to allow variable binding in if ... matches
statements.
or
b. Plan on having a future proposal to allow it.
Either way, the destination would be a place where, instead of if case
, people can write:
if foo matches .bar(let x) {
// x is usable here
}
At that point, if case
would be entirely superseded, matches
would be the pattern-matching keyword that everyone knows and uses consistently, and the language would be much better for it. We would have clarity and consistency, plus code completion would work in patterns.
If we’re going to make progress in this area, we should make progress in that direction.