Void in constrast with Unit

前端 未结 2 1234
感动是毒
感动是毒 2020-12-20 17:09

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 bu

2条回答
  •  既然无缘
    2020-12-20 17:44

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

提交回复
热议问题