On 2017-06-28 07:52, Dmitry Solomennikov wrote:
On Wednesday, 28 June 2017 at 05:01:17 UTC, Eugene Wissner wrote:
On Wednesday, 28 June 2017 at 04:41:25 UTC, Dmitry Solomennikov wrote:
Probably if you have serialized data, you convert strings to other
types, so it may be possible to perfom if-checks:
if (myDataIsStringAndDouble(data))
{
auto var = new Some!(Pair!(string, double))(new Pair!(string,
double)("df", 5.0));
}
else if (myDataIsStringAndInt(data))
{
auto var = new Some!(Pair!(string, int))(new Pair!(string,
int)("df", 5));
}
It is possible, but it is not a general solution. I've posted couple of
sample classes, but there are more complicated cases, of course, and it
well be combinatorial explosion here.
I got the Variant idea, I'll give it a try.
From other point of view, is there a reflection, say
auto i = newInstance("Pair!(int, string)(10, \"asdf\")"),
something like in Java?
It's possible to instantiate classes using reflection in D, but not for
templated classes:
class Foo(T) {}
class Bar {}
void main()
{
Foo!int a = new Foo!int;
Bar b = new Bar;
auto o1 = Object.factory("main.Foo!int.Foo"); // does not work for
templated classes
auto o2 = Object.factory("main.Bar"); // works for non-templated
classes
assert(o1 is null);
assert(o2 !is null);
}
Actually, if you have the template instantiation available you can do this:
Object o = Foo!int.classinfo.create();
--
/Jacob Carlborg