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.

Reply via email to