类型差异小 [英] Small difference in types
问题描述
我有三个应该相等的函数:
I have three functions that ought to be equal:
let add1 x = x + 1
let add2 = (+) 1
let add3 = (fun x -> x + 1)
为什么这些方法的类型不同?
add1和add3是int -> int
,但是add2是(int -> int)
.
它们都按预期工作,我只是好奇为什么FSI会以不同的方式呈现它们?
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?
推荐答案
This is typically an unimportant distinction, but if you're really curious, see the Arity Conformance for Values section of the F# spec.
我的简要总结是,(int -> int)
是int -> int
的超集.由于add1
和add3
是语法函数,因此可以推断出它们具有更特定的类型int -> int
,而add2
是函数值,因此可以推断出具有(int -> int)
类型(因此不能将其视为一个int -> int
).
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
).
这篇关于类型差异小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!