类型差异小 [英] Small difference in types

查看:53
本文介绍了类型差异小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个应该相等的函数:

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的超集.由于add1add3是语法函数,因此可以推断出它们具有更特定的类型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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆