鉴于以下代码......
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”答案......)
答案 0 :(得分:2)
在您的签名文件中,myFnTrue
和myFnFalse
具有签名IMyInterface -> bool
但在您的实施'a -> bool
中具有约束'a :> IMyInterface
(由于{{3} }},即实现是通用的,签名不是。
最简单的解决方案是将您的实施改为:
let myFnTrue i = myFn true i
let myFnFalse i = myFn false i