What's wrong with std.variant.Variant?

Steven Schveighoffer schveiguy at gmail.com
Mon Jun 15 14:03:02 UTC 2020


On 6/15/20 2:27 AM, Paolo Invernizzi wrote:
> On Sunday, 14 June 2020 at 18:04:57 UTC, Paul Backus wrote:
>> On Sunday, 14 June 2020 at 17:31:32 UTC, Dukc wrote:
>>> [...]
>>
>> FYI, SumType works with nothrow and @nogc, and can solve all of your 
>> other problems with std.variant as well. :)
>>
>> Duplicate types are not supported out-of-the-box, but can very easily 
>> be accomplished using `std.typecons.Typedef`:
>>
>>     alias InPixels = Typedef!(int[2], int[2].init, "InPixels");
>>     alias InMeters = Typedef!(float[2], float[2].init, "InMeters");
>>     alias SourceSizes = Typedef!(float[2], float[2].init, "SourceSizes");
>>     alias PictureSize = SumType!(InPixels, InMeters, SourceSizes);
> 
> +1
> 
> We are using sumtype instead of std.variant exactly for that, attributes 
> are not working with std.variant, and that's a show stopper.

I have switched mysql-native to using TaggedAlgebraic specifically 
because Variant does not support @safe.

I agree with most of the stuff in this thread. What I would like to see is:

a) Algebraic should be more supportive of merging types (probably don't 
depend on Variant guts).
b) Variant should provide a mechanism to restrict all calls on it to 
conform to specified attributes. e.g. I should be able to declare a 
SafeVariant which can only be assigned to types that support safe 
operations.

Note that even with TaggedAlgebraic I had to add a safety feature to 
ensure everything was @safe: 
https://github.com/s-ludwig/taggedalgebraic/pull/40

Note that a big user of Variant (and rightfully so) is std.concurrency. 
You can look at a lot of std.concurrency complaints to find deficiencies 
with Variant.

-Steve


More information about the Digitalmars-d mailing list