与单位相比无效 [英] Void in constrast with Unit

查看:17
本文介绍了与单位相比无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解这两个编程概念之间的区别.第一个表示没有数据类型,而后者表示类型存在但没有信息.此外,我认识到 Unit 来自函数式编程理论基础,但我仍然无法理解单元原语的可用性(例如,在 F# 程序中).

I would like to understand which is the difference between these two programming concepts. The first represents the absence of data type and at the latter the type exists but there is no information. Additionally, I recognize that Unit comes from functional programming theoretical foundation but I still cannot understand what is the usability of the unit primitive (e.g., in an F# program).

推荐答案

单位类型只是让一切变得更有规律.在某种程度上,您可以将 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# 函数类型:元组和柯里化的乐趣

这篇关于与单位相比无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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