在 F# 中,"Expecting" 和 "Given" 之间的类型不匹配错误是什么意思?



使用Binding.subModelSelectedItem((时收到以下错误:

FS0001: Type mismatch. Expecting a 'string -> Binding<(Model * 'a), b>' 
but given a
'string -> Binding<Model,Msg>'.
The type 'Model * 'a' does not match the type 'Model' 

来自以下F#代码:

type Msg =
| SetAppointmentKey  of int option

然后,在绑定中:

"SelectedAppointmentKey" |> Binding.subModelSelectedItem("AppointmentKeys", (fun m -> m.SelectedAppointmentKey), SetAppointmentKey)

我不理解错误信息。错误消息是什么意思"期望";来自谁"给定";来自什么?

很抱歉我在这里的无知,但这个新手所尝试的一切都没有解决这个问题。

谢谢你的帮助。

TIA-

我不确定代码中的具体错误在哪里,但我可以尝试帮助您理解错误消息所说的内容。一个简单的例子来说明相同的错误:

let foo (f:int -> int) = ()
let bar x = ""
foo bar

这不起作用,因为foo需要一个函数int -> int,但bar返回一个string。你得到:

错误FS0001:类型不匹配。应为int -> int,但给定int -> string类型int与类型string不匹配

错误消息告诉您用作参数的函数类型错误。它告诉您这两种类型,以及其中出错的部分(这里,返回类型不匹配(。

查看您的错误消息:

FS0001:类型不匹配。应为string -> Binding<(Model * 'a), 'b>但给定CCD_ 10。类型Model * 'a与类型Model不匹配

您似乎正在某处创建一个函数,该函数接受一个字符串并返回一个Binding。这个函数应该返回一个Binding,其中Model是第一个类型参数,但您的代码返回一个由Model和其他东西组成的元组。

如果您的代码中有fun x -> ..., something,可能在您想要(fun x -> ...), something的地方,这样的事情很容易发生。如果你写,你会得到类似的错误,例如:

let foo (f:int -> int) = ()
let bar = fun x -> 0, 1 
foo bar

最新更新