Here I have a function. In line List.append [0] free_list
it is complaining that, This expression was expected to have type unit but here has a type 'a list
, and it is not clear for me how does it infer that this statment must have an unit type.
let free_list=[]
let find_free_spaces :List<int>=
if 1=1 // The condition is not important
then List.append [0] free_list
free_list
In F#, if/then
and if/then/else
are expressions, not statements (if you're coming from C#, if x then y else z
is like x ? y : z
). If there's an else
clause, then the type of that branch must match the type of the if
branch so that the whole expression is well-typed. If there is no else
, then the if
's body must have type unit
, since otherwise the result would just be disregarded (basically, if x then y
is equivalent to if x then y else ()
).