How / when does the F # compiler read ahead to type inference?

If I create an add function below

let add x y =
    x + y

and then separately run the line below, I get an error, because F # will assume by default that x and y should be ints.

add 5.4 3.2

However, if I run them together, add the work just fine, as he now sees in it a function that receives two floats (which implies that the compiler was looking forward to make type inference).

This begs the question, why does the same search process not let F # know that toHackerTalk accepts a string? Even if I execute the lines below together, it gives me an error because the type of the phrase is undefined.

let toHackerTalk phrase =
    phrase.Replace('t', '7').Replace('o', '0')

toHackerTalk "this be strange"
+4
4

, . + ( F # ) int (, , -), float +.

, , Replace,

+1

5.2.3:

, . ,

   let f x = x + x

f, , int float. .

, , "" , . , (+), , add, inline.

, :

let d = System.Collections.Generic.Dictionary()

d.[0] <- "test"

, d Dictionary<int,string>, , , d Dictionary<obj,obj>. , d Dictionary<'a,'b> 'a 'b, , , (, , . [<GeneralizableValue>] ).

+1

, , , , , , , . , , 'a 'b . add 5.4 3.2, 'a, 'b float.

let add x y = x + y, , , int.

, , "". , , F # interactive, . , .

+1

add int. let inline add x y = x + y, x y , +. . add - int F # .

In the case, the str.Replacemethod Replaceis an instance method in a string. Thus, the compiler must know the type strin order to make any definition of its instance methods. Alternatively, in the above example, it +is a static function - the compiler knows the types and restrictions that were involved before use. More details on the withdrawal of type F #, see here .

0
source

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


All Articles