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

前端 未结 3 1403
广开言路
广开言路 2020-12-18 23:24

I read this MSDN article:

Unit Type (F#)

...The unit type is a type that indicates the absence of a specific value; the unit type has only a

3条回答
  •  隐瞒了意图╮
    2020-12-19 00:17

    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.

提交回复
热议问题