inline - f# idiomatic type resolution solution for resolving to the wrong type -
i'm getting started f# have code analogous following:
let square x = x*x let result = square 5.1 let result' = square 12 unfortunately, results in following error: this expression expected have type float here has type int
is there idiomatic f# solution problem, or thinking being tainted c# experience?
just write that:
let inline square x = x * x otherwise, after first time used square function, type inferred float -> float. hence, , given f# not automatic conversion int float, receive error.
so, if don't want use inline, simplest solution write
let result' = square (float 12) it simple , yet readable.
for more advanced solutions please take @ this: does f# have generic arithmetic support?
but solutions (imho) incomprehensible.
Comments
Post a Comment