Generic circular dependence

I 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, the interface IStateSpaceshould be something like a chessboard, and in each position of the chessboard you have a set of possible movements. These movements are called IActions here . I defined this interface so that I could adapt to various implementations: I can then define specific classes that implement the 2D matrix, 3D matrix, graphics, etc.

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

An IAction, will move up (this if in (2, 2)go on (2, 1)), go down, etc. Now I want every action to have access to StateSpace so that it can execute some validation logic. Is this implementation correct? Or is this a bad case of circular addiction? If so, how do you do the same thing differently?

thank

+3
source share
1 answer

The circular link you mentioned is not a problem. To compile the code, you need to change the interface definition IAction, though:

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

:) . , , . : , , .

+2

Source: https://habr.com/ru/post/1746479/


All Articles