Polymorphic Types F #

The following works perfectly, as one would expect in a polymorphic Curry-type system, where, for example, the identity function is actually an infinite family of functions, one for each type "a β†’ a:

let fx = x printfn "%A" (f 2) printfn "%A" (f 3.4) 

But when we try to do something more complex, it fails in the third line, "this expression should have int type, but there is a float type here":

 let fx = string x printfn "%A" (f 2) printfn "%A" (f 3.4) 

(But commenting on the second line does the work of the third line, as expected.)

A priori, I would expect that type inference will either behave in Curry style, where the undefined type is actually common, or block only one version of the function, but as far as I can see, it does the first in the first case, and the second in the second case . I assume that there is some logic of behavior that I simply do not collect. What am I missing?

+4
source share
1 answer

There is a non-standard thing with β€œhat types”. Some F # functions, such as a string, have types based on the limitations of static optimization or the existence of certain member functions. They can be generalized only in "built-in" functions, otherwise they take a monomorphic type based on use, and what happens here.

+6
source

Source: https://habr.com/ru/post/1332663/


All Articles