std.sumtype?
Paul Backus
snarwin at gmail.com
Tue Mar 30 17:14:35 UTC 2021
On Tuesday, 30 March 2021 at 17:01:29 UTC, Steven Schveighoffer
wrote:
> What might be nice is to have some simplifiers for match to
> prevent having to jump through the hoops.
>
> For example, a match flavor that throws by default, or one that
> ignores unhandled types. This way, you don't have to write
> stuff like `(_) {}` at the end of your handlers.
>
> FWIW, taggedalgebraic has `visit` to enforce all items are
> handled, and `tryVisit` that throws if at runtime it determines
> no visitors match the current type.
sumtype has had tryMatch [1] since version 0.4.0 (released May
2018).
For ignoring types, I have found the following utility function
handy:
void ignore(T)(auto ref T) {}
You can instantiate it explicitly to ignore a specific type:
obj.match!(
ignore!A,
(B b) { doSomething1; },
(C c) { doSomething2; }
);
Or you can use it as a catch-all handler:
obj.match!(
(A a) { doSomethingWith(a); },
ignore
);
[1] https://pbackus.github.io/sumtype/sumtype.tryMatch.html
More information about the Digitalmars-d
mailing list