I have a problem when returning values in complex functions. Examples are always better:
Consider the following function:
let myf (mypar: int) =
mypar + 1
Well no probel here, this function is compiled correctly and the signature is:
val myf: int -> int
OK, well. Now consider this code:
let myf (mypar: int) =
if mypar = 2 then
4 (* ERROR *)
mypar + 1
This does not work:
This expression was expected to have type unit but here has int
This error is raised everytime I try to return from my function when I am inside a if
, a while
a for
or every other block. I thought that the problem was assuring that all possible return paths return the same type, but here I do not understand what happens.
Please note that if I insert a ()
unit everything works for example:
let myf (mypar: int) =
if mypar = 2 then
() (* No error *)
mypar + 1
But that unit does not make my function return!!! it continues!!! Furthermore, could you please explain me how F# handles this???
Thankyou