std.sumtype?

Oleg B code.viator at gmail.com
Sun Aug 29 16:04:53 UTC 2021


On Sunday, 29 August 2021 at 14:25:11 UTC, Paul Backus wrote:
> On Sunday, 29 August 2021 at 14:00:30 UTC, Oleg B wrote:
>> Paul, have you any plans to update `std.sumtype` to actual 
>> version of `sumtype` (1.1.x)?
>>
>> `typeIndex` is needed
>
> I did. The change was reverted by Atila Neves:
>
> https://github.com/dlang/phobos/pull/7922
>
> He was not satisfied with the rationale given in this thread 
> for adding `typeIndex`. Since you use it yourself, you can 
> probably give a better example use-case than I did; feel free 
> to add your own comment to the discussion.

I write `sbin` and here I try to rewriter for use `std.sumtype`

https://github.com/deviator/sbin/blob/master/source/sbin/type.d#L147

like this

```d
static if (isTagged!(T).isSumType)
{
     version (Have_std_sumtype)
     {
         import std : staticIndexOf;
         // version from std doesn't have typeIndex
         return cast(TaggedTagType!T)(
             sumtype.match!(v => 
staticIndexOf!(Unqual!(typeof(v)), T.Types))(val));
     }
     else
         return cast(TaggedTagType!(T))(val.typeIndex);
}

```

but on this code I get problem:

```d
alias UT3 = SumType!(typeof(null), byte, This[]);

unittest
{
     auto val1 = UT3([ UT3(42), UT3(null),
                     UT3([ UT3(null), UT3(65) ]), UT3(12) ]);

     import std : stderr;

     auto data = sbinSerialize(val1);
     stderr.writeln(data);
     stderr.writeln([2, 4, 1, 42, 0, 2, 2, 0, 1, 65, 1, 12]);
     assert (data == [2, 4, 1, 42, 0, 2, 2, 0, 1, 65, 1, 12]);
     auto val2 = sbinDeserialize!UT3(data);
     assert (val1 == val2);
}
```

output:
```
[255, 4, 1, 42, 0, 255, 2, 0, 1, 65, 1, 12]
[2, 4, 1, 42, 0, 2, 2, 0, 1, 65, 1, 12]
... assertion ...
```

It means that `sumtype.match!(v => 
staticIndexOf!(Unqual!(typeof(v)), T.Types))(val));` doesn't 
return true value of type index (255 instead of 2).

Can you suggest how to rewrite it? If it not possible, may be it 
can be strong argument for adding `typeIndex`?

ps sumtype 1.1.4 with `typeIndex` works perfect with `UT3`.


More information about the Digitalmars-d mailing list