In F#, what does error of type mismatch between "Expecting" and "Given" mean?

566 views Asked by At

I am receiving the following error when using the 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' 

From the following F# code:

type Msg =
        | SetAppointmentKey  of int option

Then, in the bindings:

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

I do not understand the error message. What does the error message mean? "Expecting" from who? "Given" from what?

Sorry for my ignorance here, but nothing this newbie has tried has fixed this.

Thank you for any help.

TIA

1

There are 1 answers

1
Tomas Petricek On BEST ANSWER

I'm not sure where the specific error is in your code, but I can try to help you understand what the error message says. A simple example to illustrate the same error:

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

This does not work, because foo expects a function int -> int, but bar returns a string. You get:

error FS0001: Type mismatch. Expecting a int -> int but given a int -> string The type int does not match the type string

The error message tells you that the function you're using as an argument has a wrong type. It tells you the two types and also a part of it where it goes wrong (here, the return types do not match).

Looking at your error message:

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

It seems that you are creating a function somewhere that takes a string and returns a Binding. This function is expected to return a Binding with Model as the first type parameter, but your code returns a tuple formed by Model and some other thing.

Something like this can easily happen if you have fun x -> ..., something in your code, perhaps in a place where you wanted (fun x -> ...), something. You would get a similar error if you wrote, e.g.:

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