Circular dependency with generics

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-05-20T21:34:15Z Indexed on 2010/05/20 22:20 UTC
Read the original article Hit count: 264

I have defined the following interface:

public interface IStateSpace<State, Action>
where State : IState
where Action : IAction<State, Action> // <-- this is the line that bothers me
{
    void SetValueAt(State state, Action action);
    Action GetValueAt(State state);
}

Basically, an IStateSpace interface should be something like a chess board, and in each position of the chess board you have a set of possible movements to do. Those movements here are called IActions. I have defined this interface this way so I can accommodate for different implementations: I can then define concrete classes that implement 2D matrix, 3D matrix, graphs, etc.

public interface IAction<State, Action> {
    IStateSpace<State, Action> StateSpace { get; }
}

An IAction, would be to move up(this is, if in (2, 2) move to (2, 1)), move down, etc. Now, I'll want that each action has access to a StateSpace so it can do some checking logic. Is this implementation correct? Or is this a bad case of a circular dependence? If yes, how to accomplish "the same" in a different way?

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics