抽象类型定义f#

Kas*_*sen 2 c# inheritance f# abstract-class interface

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

例如:

type Interface1 =
      type MyType1
      type MyType2

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


Module MyModule =
Run Code Online (Sandbox Code Playgroud)

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

我不是在签名文件中做这一切的原因是因为我希望能够在c#中实现type1和2,但是实现了Interface1.

谁能帮我这个?

rmu*_*unn 6

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

type Interface2<'T1,'T2> =
      abstract member method1 : 'T1*'T2 -> int
Run Code Online (Sandbox Code Playgroud)

你根本不需要Interface1这里.那么,如果有Type1,并Type2在C#中实现(或F#为此事),你的C#类可以继承Interface2<Type1,Type2>和你所有的设置.

编辑:如果我理解正确的话您的意见,您要在设置一些限制'T1,并'T2使得它们实现特定的接口.所有这些('T1,Type1等等)的通用名称开始让我感到困惑,所以我将使用特定的名称作为示例.假设您有一个通用IKeyboard接口和一个通用IMouse接口,并且您希望库的用户为您的方法实现特定的键盘和鼠标类.换句话说,'T1上面的类型必须来自IKeyboard,而'T2上面的类型必须来自IMouse.在这种情况下,类型约束是您正在寻找的:

type IKeyboard = class end
type IMouse = class end

type IInputDevices =
    abstract member getInput<'K,'M when 'K :> IKeyboard and 'M :> IMouse> : 'K*'M -> int
Run Code Online (Sandbox Code Playgroud)