On 6/23/14, 6:18 PM, John Carter wrote:
I guess between perl and Ruby and Scheme etc. I got used to creating
hybrid containers....
Want a pair of [string, fileList]? Just make an Array with two items,
one a string, one and array of strings. Done.
D barfed... leaving me momentarily stunned... then Oh Yes, type safety,
Tuple's are the answer where Tuples where Tuples...
Eventually found http://dlang.org/tuple.html and more specifically the
somewhat unexpectedly named http://dlang.org/phobos/std_typecons.html
and off I went...
I do have a personal design guideline of when you adding too much
behaviour to a heterocontainer, refactor into a class.
But I guess I have never realised how often I do casually create
heterogenous containers....
Just rambling and musing.
Union types are very common (I use them every day), and IMHO it's very
nice to have them included in the language (either built-in or as a
library solution). As a library solution I would do something like this:
Union!(int, string)[] elements;
elements ~= 1;
elements ~= "hello";
auto x = elements[0].cast!(int);
// etc.
The difference with Variant is that (I think) Variant allows any kind of
type to be inserted into it, but for a Union you are just limited to a
set of types. For example, the following would be a compile-time error:
elements[0].cast!(float); // error, Union!(int, string) doesn't include
float
As a built-in way the way to use it would be much nicer, but of course
it involves too many changes for that to happen...