Re: Generics Problem
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
---910079544-1424106561-1219740030=:30818
Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8BIT
On Mon, 25 Aug 2008, Jim Garrison wrote:
Tom Anderson wrote:
On Mon, 25 Aug 2008, Jim Garrison wrote:
I'm stuck trying to implement a recursive data structure (a simple tree)
using generics, where at each level of the tree the contained object type
may vary. For example, the object contained at the root node of the tree
is of type A; all of A's children contain objects of type B. All children
of any second-level object (i.e. all the grandchildren of A) are of type
C.
How about this:
import java.util.List ;
// your A, B, C classes
class A {}
class B {}
class C {}
[snip]
interface RootNode extends Node<A, Node<B, Node<C, NullNode>>> {
}
I discarded this early, as it has to work for a tree of arbitrary depth.
This becomes unwieldy very quickly if depth > 3.
I thought you might say that!
But in that situation, i think what you're trying to do is actually kind
of meaningless. If the tree depth can vary, with different things at each
level, then since the types of the nodes depend on the types of the
values, and of their child nodes, they aren't statically knowable, and
thus you can't do this with generics, no matter how hard you try.
tom
--
If goods don't cross borders, troops will. -- Fr?d?ric Bastiat
---910079544-1424106561-1219740030=:30818--