[swift-evolution] [Pitch] Reimagining guard case/if case
Haravikk
swift-evolution at haravikk.me
Tue Oct 25 07:15:01 CDT 2016
> On 25 Oct 2016, at 12:44, Jeremy Pereira via swift-evolution <swift-evolution at swift.org> wrote:
>> If possible, I think it would be nice to make the “case” after the “if” or “guard” optional. But that would be enough imo.
>> Alternatively replacing the “~=“ with a different keyword (and dropping the “case”) could also do trick, as Haravikk suggested (I do like the “matches”).
>
> I don’t like Haravikk’s suggestion because although he calls it a keyword, "matches” would clearly be an operator and in many discussions the idea of using words as operators has been rejected for practical compilation reasons and readability reasons. The idea of using matches because it reads better in English is bogus because, once an operator becomes widely accepted, people read it as what it is. If you see `a || b`, you don’t read "a pipe pipe b", you read "a or b”. I admit I do read "a == b" as "a equals equals b” but that seems completely natural to me.
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.
On the issue of a || b that's a little different; while personally I would actually like "and" and "or" to be keywords, the operators for these are very common (practically universal), but many languages lack a pattern matching operator, and I don't recall using one that functions quite like Swift's does. Also, I haven't delved into the implementation but is it even really an operator? It doesn't seem like something I could implement myself as I can't define a mechanism for conditional binding etc., so it doesn't really seem like an operator in the traditional sense anyway.
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 dunno, although I've started using the pattern matching operator, I just don't like it.
More information about the swift-evolution
mailing list