Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:42:27+00:00 2026-06-03T08:42:27+00:00

I have two interfaces: IState and IAction . A State has a method: GetActions

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:42:29+00:00Added an answer on June 3, 2026 at 8:42 am

    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:

    type StateMachine<'State, 'Action> =
        interface
            abstract Apply : 'State * 'Action -> 'State
            abstract GetActions : 'State -> 'Action seq
        end
    

    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:

    type IState<'Action, 'State when 'Action :> IAction<'State, 'Action> and 'State :> IState<'Action, 'State>> =
        interface
            abstract GetActions : unit -> 'Action seq
        end
    
    and IAction<'State, 'Action when 'Action :> IAction<'State, 'Action> and 'State :> IState<'Action, 'State>> =
        interface
            abstract Apply : 'State -> 'State
        end
    
    // Some stupid types to illustrate how to implement the interfaces
    type State() =
        interface IState<Action, State> with
            member this.GetActions() = Seq.empty
    
    and Action() =
        interface IAction<State, Action> with
            member this.Apply s = s
    

    I hope people won’t start using the second solution and make a design pattern named after me out of it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two interfaces which are generic, IComparable<T> , with method public boolean gt(T
I have a class that instantiates two classes which implement interfaces. I want one
We have an idl file with multiple interfaces defined, two of which have someting
I have a question about those two interfaces in Java. Set extends Collection, but
I have a machine with VmWare installed which added two extra network interfaces. The
I have the following two interfaces, which are not part of an inheritance hierarchy.
In C#, if you have two base interfaces with the same method (say, F())
Say I have two interfaces, IFeaturesA, IFeaturesB. IFeaturesA has a set of signature methods.
I have two interfaces IHeaderRow , and IDetailRow I then have an object that
I have two interfaces A and B and B is extending A. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.