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.`)
}
In the example,
Object.Some
orObject.None
method (that doesn't exist unless you changeObject.prototype
) is called. Those should beOption.Some
andOption.None
.