Is std.variant useful for types only known at run time?

Chris Piker chris at hoopjump.com
Wed Sep 8 09:55:20 UTC 2021


On Wednesday, 8 September 2021 at 08:39:53 UTC, jfondren wrote:
> so I'd look at a std.sumtype of them first:

Wow, this forum is like a CS department with infinite office 
hours!

Interesting.  I presume that the big win for using std.sumtype 
over a class set is value semantics instead of reference 
semantics?

So out of curiosity, say each structure implemented a function to 
provide the desired broken-down-time, would the following 
"virtual function" style call work?

```d
import std.sumtype;
struct BDTime { int y, int m, int d, int h, int m, double s };

struct ISO8601 { BDTime bdTime(){ ... }  }
struct FloatEpoch { BDTime bdTime(){ ... } }
struct DoubleEpoch { BDTime bdTime(){ ... } }
struct LongEpoch { BDTime bdTime(){ ... }  }
alias Time = SumType!(ISO8601, FloatEpoch, DoubleEpoch, 
LongEpoch);

void main() {
     import std.stdio : writeln;
     import std.format : format;

     Time e = ISO8601();
     BDTime = e.bdTime();
}
```
or would I need to use `match!` to get the right structure type 
and then generate the internal time representation?




More information about the Digitalmars-d-learn mailing list