[swift-evolution] [Pitch] Reimagining guard case/if case

Haravikk swift-evolution at haravikk.me
Fri Oct 28 05:02:43 CDT 2016


> On 28 Oct 2016, at 10:00, Jeremy Pereira <jeremy.j.pereira at googlemail.com> wrote:
> 
> 
>> On 25 Oct 2016, at 13:15, Haravikk <swift-evolution at haravikk.me> wrote:
>> 
>> 
>> I'm inclined to disagree; the keyword wouldn't be much different from the use of the is keyword to test a type (I even suggested using it since it's shorter than matches and wouldn't require a new term), both are run-time operations except where they can be optimised away so there is some precedent for this.
> 
> That’s more an argument for changing the syntax of `is` and `as`, perhaps giving them a function like syntax like `type(of:)` not an argument for introducing more operators that break the rules. It’s all very well following precedent, but when precedent is wrong, let’s not.

Who's to say that the precedent is wrong? is and as are very convenient keywords, far more so than having to learn a symbolic operator that might not be obvious, or digging around for a function, especially when said function would really need to be on the type, thus causing it to read in reverse of what you'd expect.

Keywords have their place, you can't just declare them wrong. Plus as I said, unless there's some hidden magic I'm unaware of pattern matching isn't actually a true operator anyway, it's syntactic sugar that looks like one, so why not a keyword? I think a case has to be made for this to be an operator every bit as much as the case for using a keyword.

>> Lastly, on it being more readable the issue isn't general readability but rather discoverability. If you see "if x matches .some(let y)" or "if x is let y?" then there's some immediate context for what's going on the first time you see it. An =~ or ~= operator is less clear as it looks like a weird assignment operator (like +=), doesn't mean the same thing as the tilde operator on its own; the only clue to it being a comparison is that it's used in an if condition.
> 
> I’m not sure I like compromising readability just so that people who have no Swift experience can understand things a bit more easily.

Compromising readability how? And it's not *just* so that people with no experience can learn; I don't find =~ or ~= that appealing as I don't feel they really communicate their purpose at all, it's just a matter of learning it by rote, which I'd prefer to avoid.

> It’s a bit similar to Perl’s string pattern matching operator (=~), so to me it seems fine and it’s already part of the language.

That's missing the point; I don't think I've used Perl once since university, and I can't think of any language I've used in the past five years that has an operator like this. Obviously other people's experience will differ, but the point is that I don't think it's common enough to consider a term of art, and on its own it doesn't convey any meaning. Like I said, tilde already has a meaning for bit-flipping, which this doesn't do, so it's something that must be purely learned, unlike say += which is intuitive if you know what both = and + do.


More information about the swift-evolution mailing list