How to assign and compare arrays to SumType?

confuzzled con at fuzzled.com
Tue Jun 11 18:26:50 UTC 2024


On Tuesday, 11 June 2024 at 16:41:46 UTC, confuzzled wrote:
> Comparison between a Variant and an array is straightforward. 
> How does one accomplish the same between a SumType and an array?
>

Okay, this is what I came up with. Just a sanity check please. 
Did I do this correctly? Is there something I'm overlooking?

```d
import std.variant;
import std.sumtype;
import std.traits: isArray;

struct S
{
     SumType!(double[]) data;
     bool opEquals(T)(auto ref const T s) const
     if (isArray!T)
     {
         return data == typeof(data)(s);
     }
     void opAssign(T)(T value)
     if (isArray!T)
     {
         data = typeof(data)(value);
     }
     this(T)(T value)
     if (isArray!T)
     {
         opAssign(value);
     }
}

void main()
{
     Variant v = [1.7, 2.7, 3.7, 4.7, 5.7];
     assert(v == [1.7, 2.7, 3.7, 4.7, 5.7]);

     S s = [1.7, 2.7, 3.7, 4.7, 5.7];
     assert(s == [1.7, 2.7, 3.7, 4.7, 5.7]);
}
```

Thanks,
--confuzzled




More information about the Digitalmars-d-learn mailing list