Small difference in types

女生的网名这么多〃 提交于 2019-12-18 19:22:09

问题


I have three functions that ought to be equal:

let add1 x = x + 1
let add2 = (+) 1
let add3 = (fun x -> x + 1) 

Why do the types of these methods differ?
add1 and add3 are int -> int, but add2 is (int -> int). They all work as expected, I am just curious as to why FSI presents them differently?


回答1:


This is typically an unimportant distinction, but if you're really curious, see the Arity Conformance for Values section of the F# spec.

My quick summary would be that (int -> int) is a superset of int -> int. Since add1 and add3 are syntactic functions, they are inferred to have the more specific type int -> int, while add2 is a function value and is therefore inferred to have the type (int -> int) (and cannot be treated as an int -> int).



来源:https://stackoverflow.com/questions/5093173/small-difference-in-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!