On 6/28/17 1:52 AM, 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\")"),

No, because the class itself doesn't eixst until you instantiate it.

Certainly if you want to write out all the possible instantiations, it's possible, via the mechanisms already specified above. What I would do is create a newInstance *template* that takes a string and generates a function that would build it from that string. Then you can feed a sample file that contains all the possible instantiations to it at *compile time* (via import strings), and then you can automatically build the code that would be able to parse it. Finally, send the real file at runtime.

something like in Java?

It's important to realize that Java's generics are completely different than D's templates. They are a compile-time wrapping around a runtime construct. Of course, it's possible to mimic Java, but you won't get templates out of it, more like Variant holders.

-Steve

Reply via email to