我希望在接口(interface)或抽象类中定义几个类型,但未指定实现。 然后我想在另一个接口(interface)中继承这个接口(interface),这样我就可以在 interface2 中使用在第一个接口(interface)中定义的类型来指定我的方法。
例如:
type Interface1 =
type MyType1
type MyType2
type Interface2 =
inherit Interface1
abstract member method1 : MyType1*MyType2 -> int
Module MyModule =
我的想法是,我希望模块随后实现接口(interface) 2,因此它应该实现 MyType1 和 MyType2,以及方法 1。
我没有在签名文件中执行所有这些操作的原因是我希望能够在 C# 中也实现 type1 和 2,但要实现 Interface1。
谁能帮我解决这个问题?
最佳答案
我认为您真正想要的是使用泛型:
type Interface2<'T1,'T2> =
abstract member method1 : 'T1*'T2 -> int
你不需要Interface1
在这里。那么如果有 Type1
和 Type2
在 C# 中实现(或在 F# 中实现),您的 C# 类可以继承自 Interface2<Type1,Type2>
一切就绪。
编辑:如果我没有正确理解你的评论,你想对 'T1
设置一些限制条件。和 'T2
这样他们就实现了特定的接口(interface)。所有这些的通用名称( 'T1
、 Type1
等等)开始让我感到困惑,所以我将使用特定名称作为示例。假设您有一个通用的 IKeyboard
接口(interface)和通用 IMouse
接口(interface),并且您希望库的用户为您的方法实现特定的键盘和鼠标类。换句话说,'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
关于c# - 抽象类型定义 f#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43888294/