Why do I need to use the unit type in F# if it supports the void type?

◇◆丶佛笑我妖孽 提交于 2019-11-29 09:27:55

In C#, there is no value of type void that can be used as an argument type. Furthermore, void can't be used as a generic type argument (so for example C# needs to use parallel Func<...> and Action<...> delegate types, but F# needs only a single function type ... -> ... which can abstract over both). This ends up greatly simplifying F# programming in many cases; for example, an Async action which performs some side effects but doesn't return a value is an instance of type Async<unit>, but in C# there's no way to create a corresponding Task<void> or whatever.

See Unit Type from Wikipedia

Void type as unit type

In C, C++, C#, and Java, void expresses the empty type. The unit type in C would be struct {}, but an empty struct is forbidden by the C language specification. Instead void is used in a manner that simulates some, but not all, of the properties of the unit type, as detailed below.

Difference in calling convention

The first notable difference between a true unit type and the void type is that the unit type may always be the type of the argument to a function, but the void type cannot be the type of an argument in C, despite the fact that it may appear as the sole argument in the list.

Difference in storage

The second notable difference is that the void type, being empty, can never be stored in a record type, i.e. in a struct or a class in C/C++. In contrast, the unit type can be stored in records in functional programming languages, i.e. it can appear as the type of a field; the above implementation of the unit type in C++ can also be stored. While this may seem a useless feature, it does allow one for instance to elegantly implement a set as a map to the unit type; in the absence of a unit type, one can still implement a set this way by storing some dummy value of another type for each key.

Another way to look at it is to visualize () as a tuple with arity of 0.

Given that () is used to delimit a tuple, we get

(abc, def, xyx) a tuple with arity of 3
(abc, def) a tuple with arity of 2
(abc) a tuple with arity of 1, which can be reduced to abc
() a tuple with arity of 0, called unit

In functional languages based on the lambda calculus, a function takes a single parameter and returns a single value. Multiple parameters are supported by currying. Parameters and return values can also be tuples to support multiple values.

My interpretation of unit / (), is no values, expressed as a tuple.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!