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?