与单位无关 [英] Void in constrast with Unit

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

问题描述

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

解决方案

单位类型只是使一切更加规律。在某种程度上,您可以将F#中的每个函数都想成一个参数并返回单个结果。不需要任何参数的函数实际上将unit作为参数,并且不返回任何结果的函数返回unit作为结果。这具有多种优点;比如,在C#中,你需要一组Func委托来代表返回值的各种arities的函数,还有一些不返回值的Action委托(因为例如 Func< int,void> 不合法 - void不能用于这种方式,因为它不是真正的类型)。 >另请参阅 F#函数类型:使用元组和currying获得乐趣

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).

解决方案

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).

See also F# function types: fun with tuples and currying

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

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