std.sumtype?

Oleg B code.viator at gmail.com
Thu Mar 18 15:58:55 UTC 2021


On Thursday, 18 March 2021 at 15:08:19 UTC, Paul Backus wrote:
> [1] https://github.com/dlang/phobos/pull/7702
> [2] 
> https://forum.dlang.org/thread/dkizmcporviutijhqlcs@forum.dlang.org

Thank for links, I don't saw these news in November.

Since sumtype will be in phobos, can you explane algorithm of 
[de]serialization of general Sumtype value?

In other libs it will be simple for serialization:
1. get tag -> write tag
2. switch for all tags (final switch + static foreach by enum 
members) -> write value

example [1] 
https://github.com/deviator/sbin/blob/master/source/sbin/serialize.d#L76

and deserialization:
1. read tag
2. final switch + static foreach check tag -> read typed value -> 
write it to algebraic variable

example [2] 
https://github.com/deviator/sbin/blob/master/source/sbin/deserialize.d#L189

for sumtype I see what I need make own enum (or use number) what 
I get through match:

auto tag = val.match!(
   (Type1 t1) => 0, // or MyOwnValTag.type1
   (Type2 t2) => 1, // or MyOwnValTag.type2
   ... etc
);

next write tag, in next match write value

how to generate this match without mixins? how to get (in general 
way) type from sumtype val by own tag or number for 
deserialization?

I think these problems have a solutions, but it more complicated 
(and need write more code) than use enum tags directly.


More information about the Digitalmars-d mailing list