抽象类型定义f#

时间:2017-05-10 09:18:33

标签: c# inheritance interface f# abstract-class

我希望在接口或抽象类中定义几种类型,但未实现未实现的实现。 然后我想从另一个接口继承这个接口,这样我就可以在interface2中用第一个接口中定义的类型指定我的方法。

例如:

type Interface1 =
      type MyType1
      type MyType2

type Interface2 =
      inherit Interface1
      abstract member method1 : MyType1*MyType2 -> int


Module MyModule =

我的想法是我希望模块然后实现interface2,所以它应该实现MyType1和MyType2,以及method1。

我没有在签名文件中执行所有操作的原因是因为我希望能够在c#中实现type1和2,但是实现了Interface1。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:6)

我认为你真正想要的是使用泛型:

type Interface2<'T1,'T2> =
      abstract member method1 : 'T1*'T2 -> int

这里根本不需要Interface1。然后,如果在C#中实现了Type1Type2(或者在F#中实现了这一点),那么C#类可以从Interface2<Type1,Type2>继承而且你已经完成了设置。

修改:如果我已正确理解您的评论,您希望在'T1'T2上设置一些约束,以便它们实现特定的接口。所有这些('T1Type1等等)的通用名称开始让我感到困惑,所以我将使用特定的名称作为示例。假设您有一个通用的IKeyboard接口和一个通用的IMouse接口,并且您希望您的库的用户为您的方法实现特定的键盘和鼠标类。换句话说,上面的'T1类型必须来自IKeyboard,上面的'T2类型必须来自IMouse。在这种情况下,您需要type constraints

type IKeyboard = class end
type IMouse = class end

type IInputDevices =
    abstract member getInput<'K,'M when 'K :> IKeyboard and 'M :> IMouse> : 'K*'M -> int