两种使用自己的类型

时间:2014-04-19 17:32:05

标签: f# fluent-interface

最近我用F#开始冒险。我正在尝试创建我将在C#项目中使用的F#库。

现在我面临的问题是我有两种类型的定义(我希望)可以自己使用(我试图为c#用法创建流畅的API)。

我想如何在c#(简化示例)中使用它。

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

现在我有两种类型(在separted文件中):

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) 

现在文件顺序导致编译错误,我想知道我的案例是否有任何解决方案?还是让我放弃流畅的api想法?

1 个答案:

答案 0 :(得分:6)

将两种类型放在同一个文件中,并将定义更改为以下内容:

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)

有关详细信息,请参阅“相互递归类型”一节'在the language reference on MSDN