I’m in trouble on designing a D app. Maybe my approach is completely wrong, so I come here to you rescue me. Any suggestion, including complete rewrite, is welcome.
I have some templated types:
enum Type : byte { Message='!', Integer='@' }
struct Token (T) {
Type type;
T value;
}
alias Token!string MessageToken;
alias Token!long IntegerToken;
And I need to handle these types generically:
AnyToken genToken(bool cond) {
if (cond)
return MessageToken(Type.Message, "nighly builds");
else
return IntegerToken(Type.Integer, -42);
}
AnyToken a = genToken(true);
AnyToken b = genToken(false);
How do I achieve this effect? Edit: OOP alternatives are welcome too.
I’d have used a tagged union myself
note that there’s no static (compile time) difference between them but it’s pretty much the best on can do without inheritance
you can add some extra functions so it looks more like inheritance so you won’t need to examine the type field so much outside the struct’s functions