How to assign and compare arrays to SumType?
Anonymouse
zorael at gmail.com
Tue Jun 11 22:40:33 UTC 2024
On Tuesday, 11 June 2024 at 18:26:50 UTC, confuzzled wrote:
> 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?
It's enough to just make the whole array another
`SumType!(double[])`.
```d
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 == SumType!(double[])([1.7, 2.7, 3.7, 4.7,
5.7]));
}
```
Or better yet, to avoid redundantly spelling out the type;
```d
assert(s.data == typeof(s.data)([1.7, 2.7, 3.7, 4.7, 5.7]));
```
More information about the Digitalmars-d-learn
mailing list