SumType extraction
An Pham
home at home.com
Sat Jul 6 23:09:48 UTC 2024
On Thursday, 27 June 2024 at 18:51:19 UTC, Josh Holtrop wrote:
> Hello all. In my application I came across a desire to store an
> ordered array of handles that could point to one of several
> different objects, and it seems like the tool I want for that
> is SumType.
>
> I started with something like (simplified of course):
>
> ```d
> class Foo {}
> class Bar {}
>
My Variant package can do this type of thing
https://github.com/apz28/dlang/blob/main/source/pham/var/var_variant.d#L3430
Variant[] mixedC;
mixedC ~= new Foo();
mixedC ~= new Bar();
size_t foundCount;
foreach (v; mixedC)
{
if (auto c = v.peek!Foo)
{
foundCount++;
}
}
assert(foundCount == 1);
More information about the Digitalmars-d-learn
mailing list