Take the following example:
class Foo {
public int SomeProperty { get; }
public IEnumerable<Foo> Children { get; }
// ....
}
If another class (e.g. Bar
) implements Foo
, Foo
becomes non-recursive and Bar
instead becomes recursive.
class Bar {
public Foo { get; } // At this point Foo.Children doesn't make sense anymore
public IEnumerable<Bar> Children { get; } // How can I enforce this part
// ...
}
Currently I need to write two versions of every class, one with Children
and one without. Inheritance makes this slightly less tedious but I can't statically guarantee that the requirements will be implemented correctly. I was also thinking about some kind of monad pattern with a higher kinded type Recursive
but I can't wrap my head around it (especially since I just started learning it).
Any ideas?
Edit: I don't understand how this got marked as a duplicate. While I ended up with writing my own generic tree in the end, I didn't require it in my question and definitely didn't ask for a library. I was asking for general advice on how to approach the problem, where the solution I settled with ended up being a generic tree by chance. The fact that this question got closed after I provided my solution leads me to believe that someone read the answer, searched for trees on SO and then chose the first result, which doesn't help anyone. The "original" question doesn't even provide a solution to my question. This nothing more than laziness paired with the abuse of admin rights.