I have two interfaces:
IState and IAction.
A State has a method: GetActions – which returns a collection of IActions.
A Action has a method: Apply – which acts on a State, to return a new State.
IState takes a type parameter to control what sort of actions it returns with get actions,
IAction takes a type parameter to control what sort of states it can act on.
(By sort, i ment implementation).
I want to be able to garentee that a State only return actions that can act on a state of that same type.
type IAction<'S when 'S:>IState> =
abstract member Apply : 'S->'S
and IState<'A when 'A:>IAction<'S when 'S:> typeof(this)>> =
abstract member GetActions : seq<'A>
but obviously typeof(this) is not a thing.
How can I have a type constraint making sure my type paramerer is of type equal to the type I am defining?
A solution to avoid getting into the problem in the first place
Not a direct answer to your question, but it should solve your original problem:
This way of approaching the problem is inspired by ML’s module system
An uglier solution
If you really want to go for the two tightly coupled interfaces, you could go that way:
I hope people won’t start using the second solution and make a design pattern named after me out of it 🙂