且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与单位相比无效

更新时间:2021-06-30 23:16:55

单位类型只是让一切变得更有规律.在某种程度上,您可以将 F# 中的每个函数视为采用单个参数并返回单个结果.不需要任何参数的函数实际上以unit"作为参数,不返回任何结果的函数返回unit"作为结果.这有很多优点;首先,考虑一下在 C# 中如何需要大量Func"委托来表示返回值的各种参数的函数,以及大量不返回值的Action"委托(因为例如 Func<int,void> 是不合法的 - void 不能那样使用,因为它不是一个真正的"类型).

The unit type just makes everything more regular. To an extent you can think of every function in F# as taking a single parameter and returning a single result. Functions that don't need any parameters actually take "unit" as a parameter, and functions that don't return any results return "unit" as a result. This has a variety of advantages; for one, consider how in C# you need both a slew of "Func" delegates to represent functions of various arities that return values, as well as a slew of "Action" delegates that do not return values (because e.g. Func<int,void> is not legal - void cannot be used that way, since it's not quite a 'real' type).

另见 F# 函数类型:元组和柯里化的乐趣