std.sumtype?
drug
drug2004 at bk.ru
Thu Mar 25 15:03:37 UTC 2021
I think that `kind` vs `handler` is more like old-school vs modern
style. They are equal in general. Modern compilers are good in
optimization. I chose TaggedAlgebraic for my projects about 7 years ago
and don't remember the exact reason I was need for kind feature. In
general I agree to Steven. I can add to his post that kind lets you do:
```D
switch(kind)
{
case Kind.foo0..case Kind.foo5:
doSomething1;
break;
case Kind.bar:
case Kind.baz:
case Kind.foobar:
doSomething2;
break;
default:
doDefaultThings;
}
```
using handlers:
```D
someType.match!(
(Foo0 foo) => doSomething1,
(Foo1 foo) => doSomething1,
(Foo2 foo) => doSomething1,
(Foo3 foo) => doSomething1,
(Foo4 foo) => doSomething1,
(Bar bar) => doSomething2,
(Baz baz) => doSomething2,
(FooBar fb) => doSomething2,
(_) => doDefaultThings;
);
```
These examples are verbose equally but the first version contains less
boilerplate.
I believe both approaches are good. They complement each other.
More information about the Digitalmars-d
mailing list