Is there a way to use Object.factory with templated classes? Or some way to construct templated classes given RTTI of an instance?
Adam D. Ruppe
destructionator at gmail.com
Thu Sep 27 13:23:15 UTC 2018
On Thursday, 27 September 2018 at 05:04:09 UTC, Chad Joan wrote:
> The tree nodes are potentially very diverse, but the tree
> structure itself will be very homogeneous.
The virtual method can still handle that case!
But, if your child array of expressions is already accessible
through the base interface, I think now your code is going to
look something more like:
Expression copy(Expression e) {
// I kinda hate the cast, but create is too generic lol
// and we already know so it ok here
Expression n = cast(Expression) typeid(e).create();
foreach(child; e.children)
n.children ~= copy(child);
return n;
}
that's not too bad :)
> I'm having trouble looking this up. Could you link me to the
> docs for this?
Well, the create method is actually better anyway (and actually
documented! I just forgot about it in that first post) but you
can see more of the guts here
http://dpldocs.info/experimental-docs/object.TypeInfo_Class.html
> As above, I think this might be a very clean and effective
> solution for a different class of use-cases :) I'll keep it in
> mind though.
Yeah. And I did make one mistake: the tupleof assignment trick
wouldn't work well for references, so lol it isn't much of a deep
copy. You'd want to deep copy any arrays too probably.
But sounds like you are already doing that, yay.
More information about the Digitalmars-d-learn
mailing list