News for hackers. Stuff that matters.
Re:JavaScript Pattern Matching Proposal by bschwindHN on Wednesday April 25, 2018 @11:14AM (#16922234)
Let's see an example of your proposed alternative syntax.
Re:JavaScript Pattern Matching Proposal by codedokode on Wednesday April 25, 2018 @11:53AM (#16922727)
Here you are:

    var x = match (response) {
        case { status:200 }: true;
        case { status: 404 }: new NotFoundError;
        case Number: Math.PI;
        case SomeClass: 1;
        case /^http/: "http error";
        default: -1;
    };
Re:JavaScript Pattern Matching Proposal by Someone on Wednesday April 25, 2018 @06:26PM (#16926885)
A problem with that is that, in JavaScript, case clauses in switch statements fall through to the next clause, whereas they shouldn’t in match statements.

When reading the code, that means you need to hunt for the match or switch statement to know what happens at the end of a case clause.

IMO, if one is willing to correct historical errors, even if they are engrained, Apple’s Swift language makes the best choice here. It requires an explicit fallthrough to fall through to the following case, and goes even further than what you propose by only having a switch keyword (https://developer.apple.com/library/content/documentation/Sw...)

Re:JavaScript Pattern Matching Proposal by bschwindHN on Wednesday April 25, 2018 @12:17PM (#16923027)
I like it, especially `default` instead of `{}`

The leading curly braces in the linked proposal _are_ a bit odd to see, though the repetition of `case` isn't too fun either.

Re:JavaScript Pattern Matching Proposal by codedokode on Thursday April 26, 2018 @01:57PM (#16934055)
Braces can be used in many cases, but `case` keyword is used only in `switch` and `match` and is not ambigious. You can see what construct it is from the first word.
Re:JavaScript Pattern Matching Proposal by aylmao on Wednesday April 25, 2018 @03:06PM (#16924940)
I suspect the example in the proposal refers to an object that doesn't match any of the previous options, but is still an object.

For a default I'd expect there to be another pattern.

Re:JavaScript Pattern Matching Proposal by edflsafoiewq on Thursday April 26, 2018 @11:02AM (#16932484)
The default pattern is a variable name.

> [About variable name patterns binding to the name] Eliminates the need for an else/default leg, because assignment to any variable will be sufficient. JS programmers are already used to assigning variables that are then ignored (in functions, in particular), and different people have different tastes in what that should be. _, other, etc, would all be perfectly cromulent alternatives.

Re:JavaScript Pattern Matching Proposal by edflsafoiewq on Wednesday April 25, 2018 @04:00PM (#16925464)
I don't like it. This is a false consistency. It looks like a switch but works totally differently (match is semantically more like an if-else chain). The proposal author gives a similar rejection of this syntax under the section "=> for leg bodies" in the proposal.
Re:JavaScript Pattern Matching Proposal by phrz on Wednesday April 25, 2018 @11:29AM (#16922426)
I think an example would resemble how Swift integrated pattern matching into their regular switch statements, it doesn't require too much creativity to envision.
Re:JavaScript Pattern Matching Proposal by 1ris on Wednesday April 25, 2018 @11:26AM (#16922371)
That's not how it works. You don't have to be Steven Spielberg to dislike some Hollywood movie. How is this fallacies called?
Re:JavaScript Pattern Matching Proposal by lghh on Wednesday April 25, 2018 @11:29AM (#16922422)
There's a totally different barrier of entry for that than suggesting a syntax. That's not even in the same stratosphere.
Re:JavaScript Pattern Matching Proposal by 1ris on Wednesday April 25, 2018 @12:15PM (#16923005)
There was a suggestion: Make it like switch/case.