variant visit not pure?

Simen Kjærås simen.kjaras at gmail.com
Thu May 7 10:41:01 UTC 2020


On Thursday, 7 May 2020 at 09:22:28 UTC, learner wrote:
> Good morning,
>
> Is there a reason why std.variant.visit is not inferring pure?
>
> ```
> void test() pure {
>     Algebraic!(int, string) alg;
>     visit!( (string) => 0, (int) => 0)(alg);
> }
>
> Error: pure function test cannot call impure function 
> test.visit!(VariantN!(16LU, int, string)).visit
> ```

std.variant.Algebraic is essentially a std.variant.Variant in 
different clothes. Variant is very flexible, and this comes at a 
cost (and isn't used in Algebraic, meaning you pay for things you 
don't use). Like Dukc said, you might be better off with 
Taggedalgebraic or SumType 
(https://code.dlang.org/packages/sumtype).


Variant uses runtime type information to hold *any* type. Since 
Algebraic specifically only holds a few types, all the framework 
that's in place for Variant is wasted on Algebraic, and makes it 
less useful and less performant.

--
   Simen


More information about the Digitalmars-d-learn mailing list