2

鉴于以下代码...

type IMyInterface =
    abstract BoolA : bool
    abstract BoolB : bool
let myFn _boolVal (_i: IMyInterface) = if _boolVal then _i.BoolA else _i.BoolB
let myFnTrue = myFn true
let myFnFalse = myFn false

... Intellisense 抱怨,并且编译器失败,如果我在其中创建一个签名文件:

type IMyInterface =
    abstract BoolA : bool
    abstract BoolB : bool
val myFnTrue : (IMyInterface -> bool)
val myFnFalse : (IMyInterface -> bool)

错误是Error 10 Module 'MyModule' contains val myFnTrue : ('_a -> bool) when '_a :> MyModule.IMyInterface but its signature specifies val myFnTrue : (MyModule.IMyInterface -> bool) The types differ。(报告了类似的错误myFnFalse。)

我觉得自己像个白痴,无法弄清楚这一点。我究竟做错了什么?(支持“duh”的答案......)

4

1 回答 1

2

在您的签名文件中,myFnTrue并且myFnFalse具有签名IMyInterface -> bool但在您的实现'a -> bool中具有约束'a :> IMyInterface(由于自动泛化),也就是说,实现是通用的,而签名不是。

最简单的解决方案是将您的实现更改为:

let myFnTrue i = myFn true i
let myFnFalse i = myFn false i
于 2013-02-15T21:46:05.257 回答