How to assign and compare arrays to SumType?

confuzzled con at fuzzled.com
Tue Jun 11 16:41:46 UTC 2024


Comparison between a Variant and an array is straightforward. How 
does one accomplish the same between a SumType and an array?

```d
import std.variant;
import std.sumtype;
import std.stdio;

struct S
{
     SumType!(double[]) data;  // {1}
}

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;
     s.data = [1.7, 2.7, 3.7, 4.7, 5.7]; // {2}
     assert(s.data == [1.7, 2.7, 3.7, 4.7, 5.7]);
}
```

Resulting Error:

> var.d(17): Error: template \`opEquals\` is not callable using 
> argument types `!()(double[])`
> /Users/anju/dlang/dmd-2.109.0-beta.1/osx/bin/../../src/phobos/std/sumtype.d(712):        Candidate is: `opEquals(this This, Rhs)(auto ref Rhs rhs)`
>  with `This = SumType!(double[]),
>       Rhs = double[]`
>  must satisfy the following constraint:
>`       !is(CommonType!(This, Rhs) == void)`

Also, assuming that {1} read "SumType!(double)[] data;", what 
would be the proper way to accomplish the assignment at {2} and 
the subsequent comparison.

Thanks,
--confuzzled

P.S. Is news.digitalmars.com still operational? I'm unable to 
access it through Thunderbird.


More information about the Digitalmars-d-learn mailing list