Search code examples
f#fluent-interface

Two types that use themselves


recently I started my adventure with F#. I'm trying to create F# library that I will use in my C# projects.

Now I'm facing the problem that I have two types definitions that (as I wish) could use themselves (I'm trying to create fluent API for c# usage).

How I want to use it in c# (simplified example).

Shopping shopping = new Shopping(); 
Stuff[] stuff = shopping.GoTo("Wallmart").Buy(new [] { "Candies", "Ice cream", "Milk" }).GoTo("Drug store").Buy(new [] { "Anvil" }).GetStuff();

Now I have two types (in separted files):

type ShopResult(someContext: ShoppingContext) =
 //some logic
 member this.GoTo shopName = new ToDoResult(someContext)

type ToDoResult(someContext: ShoppingContext) = 
 //some logic
 member this.Buy what = new ShopResult(someContext) 

Now the file order causing compilation error and I'm wondering if there's any solution for my case? or have I to drop the fluent api idea?


Solution

  • Put both types in the same file and change the definitions to the following:

    type ShopResult(someContext: ShoppingContext) =
        //some logic
        member this.GoTo shopName = new ToDoResult(someContext)
    
    and ToDoResult(someContext: ShoppingContext) = 
        //some logic
        member this.Buy what = new ShopResult(someContext)
    

    For more information, see the section 'Mutually Recursive Types' in the language reference on MSDN.