Template match
Tries to match handlers by deconstructing a type if it's deconstructable, and calling the appropriate handler for the deconstructed value. If it's not a deconstructible type then it just tries a first match
template match(handlers...)
;
The currently supported deconstructible types are:
You may not handle destructible types and non destructible types in the same list of handlers. See example for usage details
Contained Functions
Name | Description |
---|---|
match | Expect match: Pass two lambdas to the match function. The first one handles the expected case and the second one handles the unexpected case. |
match | Try match: Pass two lambdas to the match function. The first one handles the success case and the second one handles the failure case. Calling match will execute the try function if it has not already done so |
match | Optional match: Pass two lambdas to the match function. The first one handles the some case and the second one handles the none case. |
match | Non-deconstructible type match: Pass n lambdas as handlers and the first one that matches the value type will be called. |
Parameters
Name | Description |
---|---|
handlers | lambdas to the type handlers. |
Since
- 0.12.0
Example
import ddash .utils: Try, some, Expect, Unexpected;
const a = Try!(() => 3) .match!(
(int) => true,
(Exception) => false,
);
const b = Expect!(int, int)(3) .match!(
(int) => true,
(Unexpected!int) => false,
);
const c = some(3) .match!(
(int) => true,
() => false,
);
assert(a);
assert(b);
assert(c);