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

user2402179

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 single value, which acts as a placeholder when no other value exists or is needed ... The unit type resembles the void type in languages such as C# and C++...

So... Alright, I understand, that the unit type is such a type, which has only a single value (). But I have some questions:

  1. Why is it needed?
  2. When is it needed?

I don't understand why not to use the void type in F#, like C# and C++ use.

If I look at the following table:

Primitive Types (F#)

Type   .NET Type    Description  
void   Void         Indicates no type or value.

I see that F# does have a void type. So I don't understand why the unit type needed; it looks like it is very similar to void.

I suppose that it relates to the functional language paradigm and that's why it's needed, so please... explain more about this to me.

kvb

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Why do I need depthBuffer to use RenderTexture?

来自分类Dev

Do I need to set type face-manually to roboto-light if i am targeting api 11+ or can I just use android:fontFamily="sans-serif-light"

来自分类Dev

Why do I need to update uvs?

来自分类Dev

Why use void(0)?

来自分类Dev

What do I need to do to use tgmath on iOS?

来自分类Dev

Why do i need the gil for PyMem_Malloc()?

来自分类Dev

Why do I need another constructor in an extended abstract class?

来自分类Dev

Do I need STUN?

来自分类Dev

未知语法void(Type :: m_function)()

来自分类Dev

I'm getting an error "invalid use of incomplete type 'class map'

来自分类Dev

How can I use a type synonym in an instance declaration?

来自分类Dev

How do I change the property of one type of Child Controls in a form?

来自分类Dev

找到scala所需的单位Unit.type

来自分类Dev

函数指针不兼容的指针类型从void(my_type *)变为void(*)(void *)

来自分类Dev

Why do we need to use import 'babel-polyfill'; in react components?

来自分类Dev

Linux find -type f 被忽略

来自分类Dev

How do I know if my MacBook Pro supports SATA III?

来自分类Dev

将UnsafeMutablePointer <Void>转换为UnsafeMutablePointer <#Struct type#>

来自分类Dev

(void *)和(void(*)(argument type))强制转换有什么区别?

来自分类Dev

Example of type in System F that is not available in Hindley Milner type inference

来自分类Dev

错误:类型不匹配;找到:Unit.type必需:单位

来自分类Dev

How do I get the System.Type of a static member using reflection?

来自分类Dev

How to use unscoped enumerator as if its type is its underlying type

来自分类Dev

Do I need to free popup menus in GTK?

来自分类Dev

Which keys do I need for CNContactFormatter?

来自分类Dev

Type of f g x = g . g x

来自分类Dev

Need "Method return type" of data selected with LINQ from DataTable

来自分类Dev

为什么不能分配I <?将Type>扩展为<Type>?

来自分类Dev

为什么不能分配I <?将Type>扩展为<Type>?

Related 相关文章

  1. 1

    Why do I need depthBuffer to use RenderTexture?

  2. 2

    Do I need to set type face-manually to roboto-light if i am targeting api 11+ or can I just use android:fontFamily="sans-serif-light"

  3. 3

    Why do I need to update uvs?

  4. 4

    Why use void(0)?

  5. 5

    What do I need to do to use tgmath on iOS?

  6. 6

    Why do i need the gil for PyMem_Malloc()?

  7. 7

    Why do I need another constructor in an extended abstract class?

  8. 8

    Do I need STUN?

  9. 9

    未知语法void(Type :: m_function)()

  10. 10

    I'm getting an error "invalid use of incomplete type 'class map'

  11. 11

    How can I use a type synonym in an instance declaration?

  12. 12

    How do I change the property of one type of Child Controls in a form?

  13. 13

    找到scala所需的单位Unit.type

  14. 14

    函数指针不兼容的指针类型从void(my_type *)变为void(*)(void *)

  15. 15

    Why do we need to use import 'babel-polyfill'; in react components?

  16. 16

    Linux find -type f 被忽略

  17. 17

    How do I know if my MacBook Pro supports SATA III?

  18. 18

    将UnsafeMutablePointer <Void>转换为UnsafeMutablePointer <#Struct type#>

  19. 19

    (void *)和(void(*)(argument type))强制转换有什么区别?

  20. 20

    Example of type in System F that is not available in Hindley Milner type inference

  21. 21

    错误:类型不匹配;找到:Unit.type必需:单位

  22. 22

    How do I get the System.Type of a static member using reflection?

  23. 23

    How to use unscoped enumerator as if its type is its underlying type

  24. 24

    Do I need to free popup menus in GTK?

  25. 25

    Which keys do I need for CNContactFormatter?

  26. 26

    Type of f g x = g . g x

  27. 27

    Need "Method return type" of data selected with LINQ from DataTable

  28. 28

    为什么不能分配I <?将Type>扩展为<Type>?

  29. 29

    为什么不能分配I <?将Type>扩展为<Type>?

热门标签

归档