Given the following code...
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 complains, and the compiler fails, if I create a signature file with this in it:
type IMyInterface =
abstract BoolA : bool
abstract BoolB : bool
val myFnTrue : (IMyInterface -> bool)
val myFnFalse : (IMyInterface -> bool)
The error is 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
. (A similar error is reported for myFnFalse
.)
I feel like an idiot, not being able to figure this out. What am I doing wrong? (Bracing for the "duh" answer...)
In your signature file, myFnTrue
and myFnFalse
have the signature IMyInterface -> bool
but in your implementation 'a -> bool
with the constraint 'a :> IMyInterface
(due to automatic generalization), that is, the implementation is generic and the signature is not.
The simplest solution is changing your implementation to this:
let myFnTrue i = myFn true i
let myFnFalse i = myFn false i