News for hackers. Stuff that matters.
Re:JavaScript Pattern Matching Proposal by codedokode on Wednesday April 25, 2018 @11:08AM (#16922170)
What an ugly syntax. They should follow switch/case syntax for consistency.
Re:JavaScript Pattern Matching Proposal by Analemma_ on Wednesday April 25, 2018 @11:13AM (#16922221)
Pattern matching doesn't work the same way as switch/case though. Using that syntax would be misleading and confusing.
Re:JavaScript Pattern Matching Proposal by maemilius on Wednesday April 25, 2018 @11:35AM (#16922498)
I think there's an argument to be made that switch statements already do something adjacent to pattern matching. At the least, it's close enough that something like:

  switch(expr) {
    case 'foo':
      break;
   case { foo: bar }:
      break;
  }
Wouldn't strike me as all that strange. It just shifts the semantics from "are you exactly this" to "do you look like this". For non-object primitives (e.g. string or number), I don't think those two things are functionally different.

Granted, it doesn't help make switch any easier to learn, but I _do_ think it makes switch more _rewarding_ to learn. Right now, switch is basically just a restrictive, potentially terser version of an if statement. This would make switch actually useful to learn.

It might even allow us to add some more semantics to switch over time (e.g. constructor matching with something like 'case Number').

(EDIT: thanks to armandososa for teaching me a new thing!)

Re:JavaScript Pattern Matching Proposal by aylmao on Wednesday April 25, 2018 @03:01PM (#16924886)
I'd argue we should strive to move away from the switch syntax where it's not necessary because it's verbose and has curious semantics [1]. As an example, this should work:

  switch(expr) {
    case 'foo':
      console.log("a string")
      break;
    case { foo: bar }:
      console.log("foo is", bar)
      break;
  }
But how would this work?

  switch(expr) {
    case 'foo':
    case { foo: bar }:
      console.log("foo is", bar)
      break;
  }
Moreover, a big selling point of pattern matching is it is an expression. Keep in mind though case points to a statement list, How do we resolve to a value?

With a "return"?

  function f() {
    switch(expr) {
      case 'foo':
        return 4 // This makes f return 
    }
  }

  function g() {
    var x = switch(expr) {
      case 'foo':
        return 4 // But this doesn't. Is this confusing?
    }
  }
The value of the last statement?

  function g() {
    var x = switch(expr) {
      case 'foo':
        4; // This should resolve to 4
        break; // but is the break necessary now? 
    }
  }
IMO switch sytnax is just legacy left behind by C, and not the best one to keep around, especially given the semantics of pattern matching.

[1]: https://en.wikipedia.org/wiki/Duff%27s_device, not sure if this works on JavaScript (I doubt it), but the point is the switch cases essentially work like "goto"s.

Re:JavaScript Pattern Matching Proposal by armandososa on Wednesday April 25, 2018 @12:22PM (#16923094)
You'll need to insert a newline and 2 spaces before a code block to be recognized as such:

    switch(expr) {
       case 'foo': break;
       case { foo: bar }: break; 
    }
https://news.ycombinator.com/formatdoc
Re:JavaScript Pattern Matching Proposal by maemilius on Wednesday April 25, 2018 @12:45PM (#16923375)
Ah, thanks. I should have guessed it would work something like that.
Re:JavaScript Pattern Matching Proposal by masklinn on Wednesday April 25, 2018 @12:57PM (#16923518)
C's switch/case is essentially a special case with a limited set of values (literals) and no destructuring. You can extend it to less trivial patterns and blam pattern matching.
Re:JavaScript Pattern Matching Proposal by edflsafoiewq on Wednesday April 25, 2018 @04:17PM (#16925619)
No, switch is not like a special case of match. C's switch is a kind of computed goto and can build irreducible control flow graphs. match is a reducible structured control-flow construct whose power comes from destructuring. match is much, much more like an if-else chain than a switch.
Re:JavaScript Pattern Matching Proposal by masklinn on Thursday April 26, 2018 @08:31AM (#16931218)
> No, switch is not like a special case of match.

Sure is. Or a degenerate one if you prefer.

> match is a reducible structured control-flow construct whose power comes from destructuring.

Match on sum types with no associated data and you have a switch.

> match is much, much more like an if-else chain than a switch.

Depends on what you're matching.

Re:JavaScript Pattern Matching Proposal by edflsafoiewq on Thursday April 26, 2018 @10:49AM (#16932336)
No it isn't. Again, switch can build irreducible control flow, match cannot. This code

    switch (x) {
    case 0:
        while (i < l) {
            s *= i;
    case 1:
            i += 1;
        }
    }
cannot be rewritten with a simple match. You'd have to reloop the CFG.
Re:JavaScript Pattern Matching Proposal by k_ on Wednesday April 25, 2018 @11:27AM (#16922388)
Why not? In Haxe (a statically typed language which compiles to js, amongst other targets) pattern matching happens in a switch: https://haxe.org/manual/lf-pattern-matching-structure.html
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.
Re:JavaScript Pattern Matching Proposal by metalrain on Wednesday April 25, 2018 @11:31AM (#16922448)
I think match cases should be functions, since you have built those anyway.

So instead of match (x) { expr... } you would have something like match(x, [ fn... ]) or match(x) { name: fn, name2: fn2 } where fn is like (objectToMatch) => { expr }

Re:JavaScript Pattern Matching Proposal by codedokode on Wednesday April 25, 2018 @11:46AM (#16922636)
It could look like this:

    var x = match (response) {
        case { status:200 }: true;
        case { status: 404 }: false;
        case Number: 0;
        case SomeClass: 1;
        default: -1;
    };
Re:JavaScript Pattern Matching Proposal by ThurmaUman on Wednesday April 25, 2018 @12:24PM (#16923111)
so make switch first class?

    var x = (switch(true){
        case response === { status: 200 }:
            return true;
        case response === { status: 404 }:
            return false;
        case response === Number:
            return 0;
        case response === SomeClass: 
            return 1;
        default:
            return -1;
    });
Re:JavaScript Pattern Matching Proposal by masklinn on Wednesday April 25, 2018 @12:55PM (#16923498)
> so make switch first class?

No. That would make the actual switch statement a value, rather than make switches expressions. And your version is completely different as you make cases into guards rather than labels or patterns.

Re:JavaScript Pattern Matching Proposal by kbp on Thursday April 26, 2018 @01:42AM (#16929107)
This would introduce a weird inconsistency with cases for match and cases for switch regarding fallthrough, though. If it's going to look like cases in a switch statement, it should act like them, for better or worse.