Can I place a dynamic array, an associative array and a string to a union and work with all elements? They are built-in types, but actually not primitive ones (this is not allowed in C++ for example). That's why I'm in doubt. I'm trying to implement something close to variant type (with a few restricted underlying types) for education purposes.

union Foo {
    int[] array;
    int[string] map;
    string str;
}

Foo u;

u.array = [];
u.array ~= 20;
u.array ~= 31;
writeln(u.array);

u.map = (int[string]).init;
u.map["test"] = 20;
u.map["value"] = 31;
writeln(u.map);

u.str = "test";
writeln(u.str);

This code works as I expected but I'm unsure that it's correct.

Reply via email to