tc39 / proposal-pattern-matching

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

Seemingly typo in Arglist Patterns Examples #327

Closed NagayamaToshiaki closed 6 months ago

NagayamaToshiaki commented 6 months ago

In the example, Object.Some or Object.None method (that doesn't exist unless you change Object.prototype) is called. Those should be Option.Some and Option.None.

class Option {
  constructor() { throw new TypeError(); }
  static Some = class extends Option {
    constructor(value) { this.value = value; }
    map(cb) { return new Option.Some(cb(this.value)); }
    // etc
    static [Symbol.customMatcher](subject) {
      if (subject instanceof Option.Some) { return [subject.value]; }
      return false;
    }
  };

  static None = class extends Option {
    constructor() { }
    map(cb) { return this; }
    // Use the default custom matcher,
    // which just checks that the subject matches the class.
  };
}

let val = Option.Some(5);
match(val) {
  when Option.Some(String and let a): console.log(`Got a string "${a}".`);
  when Option.Some(Number and let a): console.log(`Got a number ${a}.`);
  when Option.Some(...): console.log(`Got something unexpected.`);
  // Or `Option.Some`, either works.
  // `Option.Some()` will never match, as the return value
  // is a 1-item array, which doesn't match `[]`
  when Option.None(): console.log(`Operation failed.`);
  // or `Option.None`, either works
  default: console.log(`Didn't get an Option at all.`)
}