views:

49

answers:

1

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

+2  A: 

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.

Thorarin
Ah. So what really are circular dependencies? If I get you right and this is a circular reference, aren't they a problem at all? ty
devoured elysium
The circular dependency problem refers to assemblies. It is what you would need for a circular reference between two classes in different assemblies. Supposedly it is possible with some tricks, but it can usually be solved in a cleaner way.
Thorarin