Why does F # output int in this function?

In F #, I can combine strings with an operator +like this:

let myString = "one" + "two"

But when I create a function that takes two arguments and applies the same operator, and I put it in the module declared before using it, F # writes the types as integers:

module StringFunctions = 

let add str1 str2 = 
    str1 + str2

The call below gives me a build error: "It is expected that the expressions will be of type" int ", but there is a type of" string "here:

let str3 = StringFunctions.add "three" "four"

Why does F # issue this as an int? I assume that this cannot be a general method, because not all types will implement the operator +, but why does it accept an int?

+4
source share
1 answer

: " F #".

, ( , , +), F # . + , F # , + , .

, , - :

let add (str1 : string) str2 = str1 + str2

, : , + , , F # , .

, , inline, :

  • F # ,
  • F # + , .

add 3 5 3 + 5, , add "foo" "bar" "foo" + "bar", +.

, , , F # generics/function overloading syntax `inline` F # ( ), .

+5

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


All Articles