Chooses and executes a function for each variant in the Result structure.
('a is Variant).({ 'b: (Object Any) => 'c }) => 'c
where 'b = 'a[`@@folktale:adt:tag]
Chooses and executes a function for each variant in the Result structure.
const Result = require('folktale/result');
Result.Ok(1).matchWith({
Ok: ({ value }) => `ok ${value}`,
Error: ({ value }) => `error ${value}`
});
// ==> 'ok 1'
Result.Error(1).matchWith({
Ok: ({ value }) => `ok ${value}`,
Error: ({ value }) => `error ${value}`
});
// ==> 'error 1'
matchWith(pattern) {
assertObject(`${typeId}'s ${name}#matchWith`, pattern);
if (name in pattern) {
return pattern[name](this);
} else if (ANY in pattern) {
return pattern[ANY]();
} else {
throw new Error(getMatchWithErrorMessage(pattern, name));
}
}