Type inference in ToString () vs string command

Note. This question is somewhat related to my previous one , but it really touches on the problem from different points of view.

Consider the following snippet:

let toStr a = a.ToString()
let strOp a = string a

let intToStr = 5 |> toStr
let floatToStr = 5.0 |> toStr

let intStrOp = 5 |> strOp
let floatStrOp = 5.0 |> strOp //type inference error

While the function strOpuses what seems like a more elegant solution, and can also convert the unit value to a string, it does not seem to be really universal, as its type becomes limited during first use (even the type is inferred obj -> string, not 'a -> string)

Why doesn't the string operator work in this general way? Or am I doing something wrong?

+3
source share
1 answer

, string (. ), ToString - , , ToString , .

() -, ( ) let -bound-:

  • let -bound, .

  • , . , , inline, , .

, , string , , ToString , null - toStr null , strOp null !

+4

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


All Articles