SumType extraction
Josh Holtrop
jholtrop at gmail.com
Fri Jun 28 12:43:46 UTC 2024
On Friday, 28 June 2024 at 10:52:01 UTC, drug007 wrote:
> What prevents you from doing:
> ```D
> import std.sumtype;
>
> class Foo {}
> class Bar {}
>
> alias Item = SumType!(Foo, Bar);
>
> void main()
> {
> Item[] items = [Item(new Foo()), Item(new Bar()), Item(new
> Foo()), Item(new Bar())];
> foreach (item; items)
> {
> item.match!(
> (Foo v) { /* do something with foo */ },
> (_) {}
> );
> }
> }
> ```
> ?
> It's more effective by the way - you check the type once only.
Nothing prevents that, and indeed I still plan to use item.match!
like that when I need to handle multiple/all types. I just wanted
the get! functionality when I only expect or want to handle one
type without all the additional pattern matching syntax.
But, I think my:
```d
if (Foo foo = item.get!Foo)
{
/* do something with foo */
}
```
is still only checking the type once due to the one call to
match! in get!, right?
More information about the Digitalmars-d-learn
mailing list