tc39 / proposal-pattern-matching

Pattern matching syntax for ECMAScript
https://tc39.es/proposal-pattern-matching/
MIT License
5.5k stars 89 forks source link

With chaining example could be more clear #244

Closed Pyrolistical closed 2 years ago

Pyrolistical commented 2 years ago

Here is the with chaining example.

class MyClass = {
  static [Symbol.matcher](matchable) {
    return {
      matched: matchable === 3,
      value: { a: 1, b: { c: 2 } },
    };
  }
};

match (3) {
  when (${MyClass}): true; // matches, doesn’t use the result
  when (${MyClass} with {a, b: {c}}): do {
    // passes the custom matcher,
    // then further applies an object pattern to the result’s value
    assert(a === 1);
    assert(c === 2);
  }
}

I understand it is intended to explain the with chaining, but it is ill-formed.

I am fairly certain, the first when clause will always match and second when clause never matches. I believe the second when clause is there just to show the with chaining syntax. However overall it is a non-sensentical match expression. It would have been more clear by having two separate match expressions.

Also MyClass = { is invalid syntax.

tabatkins commented 2 years ago

Yes, the second match clause is subsumed by the first, but as you note, it's meant to just illustrate the behavior. Multiple match expressions could be used, but I don't think they'd meaningfully improve the example, just make it longer. Both clauses are already "useless", after all - the match expression is being used as a statement, but neither clause has side effects.