There are many cases where you do not know in advance what type
of data is being received through an interface to some other
system. This happens with some databases, and with messaging
systems, and if you are interfacing to any dynamically or weakly
typed language, such as with js/json.
I will be looking at what you did closely, so thanks for sharing!
I'll also look at the interpreter, many uses for something like
that too.
Originally to solve these interfacing problems, I wrote a
property tree implementation in C++ that used a dynamically typed
variant similar to your "var" type. With a property tree, you can
dynamically generate any hierarchical structure to multiple and
varying depths, great to have when you have no idea what the
structures will look like in advance.
For the variant type, I added in protection for strong typing,
but it could be by-passed with automatic conversion (if possible)
between types. I used it to create a generalized API for sqlite
databases. Later on it was used for interfacing with messages
recvd/sent via json over tcp.
Later after deciding to switch to D, the first thing I did was to
re-write a similar property tree system in D, and the experience
was much better although with one notable exception: Unlike in
C++, I could not create conversion operators that infer the
receiving type when doing assignments from the variant, for
example:
int i;
char c;
i = var;
c = var;
in C++ the correct "getter" for returning int or char will be
called
eg
operator int(){...}
operator char(){...}
In D, I have to resort to something a manual system like this:
i = var.get!int;
c = var.get!char;
Not nearly as nice.
I suppose i could to this
c = var.get!(typeof(c));
Safer but very ugly and tedious.
My hope is if multiple alias ever arrives it will solve the
conversion issue. Do you have any insight on how to solve the
conversion problem using current D, and if you think multiple
alias this will solve the problem?
--rt