Hi list, this one got me thinking for the last week, and still I can't find a suitable solution for it.
After a bunch of (failed) experiments I'm hoping to find some help here...
The problem goes like this:
1. I have a tree of Nodes
2. each node can have a list of children (no cycles!, and there's a root)
3. a Node has subclasses (in my case Element extends Node, and Document extends Element)
4. the children array can contain nodes of any of these subclasses
5. Node and subclasses use different parameters in the respective constructors (this could be changed, but might be worthy pointing it out)
6. I want to be able to call clone() on any type of Node (so also a subclass of it) and get back a deep copy of it (BUT with each node cloned of the correct type)
Don't know if it's worth mentioning, but this is related to translating clone() functionality from java.
I've tried many things hoping to solve this (generics, static copy(), extensions, etc. ), but just can't wrap my head around it.
(f.e.: I've tried using a cloneInto(T), but that could not be extended, as the superclass would have a different signature)
I realize the exposition of my problem so far might be lacking. Please ask me anything so I can clarify.
And since I'm sure I must be doing something wrong in my attempts (which are all messily scattered in my HD), any help is more than welcome!