Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular dependency with generics

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

like image 354
devoured elysium Avatar asked Nov 06 '22 13:11

devoured elysium


1 Answers

The circular reference you've pointed out is not a problem. For your code to compile, you will need to amend your IAction interface definition though:

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

How's that for circular reference :) Generally the compiler will handle them by using placeholders. In the case of generic type constrains, this probably isn't even necessary. One small note: if you define a circular reference between classes that aren't in the same assembly, it will become a problem.

like image 174
Thorarin Avatar answered Nov 13 '22 06:11

Thorarin