mercredi 23 janvier 2019

A class is recursive - when this class gets implemented, the parent class becomes recursive

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?

Aucun commentaire:

Enregistrer un commentaire