Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't there implicit conversions in F#?

Tags:

f#

F# does not support implicit conversions. I understand that this is a feature, but I don't understand why implicit conversions are forbidden even when no information would be lost. For example:

sqrt 4      // Won't compile.

I don't see a problem implicitly converting the int 4 to a float, which is what sqrt requires.

Can anyone shed light on this?

like image 724
royco Avatar asked Nov 06 '10 02:11

royco


Video Answer


1 Answers

Because its type checker almost depends on classical strong type-reconstruction. You example requires the coercion of types which is possible through implicit casts or a weak type system, but these aren't allowed in this kind of type inference.

Since F# comes from OCaml it has a type reconstruction that tries to guarantee correctness of your program by being extremely pedantic: the algorithm tries to unify the whole types of your program to a good typing and this cannot be done if a weak type rule allows to consider an integer like a float.

like image 100
Jack Avatar answered Sep 29 '22 10:09

Jack