I'm just getting started with F# but I have some code that is analogous to the following:
let square x = x*x
let result = square 5.1
let result' = square 12
Unfortunately, this results in the following error: This expression was expected to have type float but here has type int
Is there an idiomatic F# solution to this problem, or is my thinking being tainted by my C# experience?
Just write it like that:
Otherwise, after first time you used that
square
function, it type was inferred to befloat -> float
. Hence, and given that F# does not do automatic conversion fromint
tofloat
, you receive an error.So, if you don't want to use
inline
, the simplest solution is to writeIt is simple and yet readable.
For more advanced solutions please take a look at this: Does F# have generic arithmetic support?
But those solutions are (imho) incomprehensible.