Is std.variant useful for types only known at run time?
Steven Schveighoffer
schveiguy at gmail.com
Wed Sep 8 11:31:36 UTC 2021
On 9/8/21 5:55 AM, Chris Piker wrote:
> 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?
>
>
Just as an aside,
[taggedalgebraic](https://code.dlang.org/packages/taggedalgebraic) does
this for you.
-Steve
More information about the Digitalmars-d-learn
mailing list