gpt4 book ai didi

f# - 如果F#支持void类型,为什么我需要使用单位类型?

转载 作者:行者123 更新时间:2023-12-04 00:19:25 26 4
gpt4 key购买 nike

我读了这篇 MSDN 文章:

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



所以...好吧,我明白了,单位类型就是这样一种类型,它只有一个值 () .但我有一些问题:
  • 为什么需要它?
  • 什么时候需要?

  • 我不明白为什么不在 F# 中使用 void 类型,就像 C# 和 C++ 使用一样。

    如果我看下表:

    Primitive Types (F#)
    Type   .NET Type    Description  
    void Void Indicates no type or value.

    我看到 F# 确实有一个 void 类型。所以我不明白为什么需要单位类型;看起来它与void非常相似。

    我想它与函数式语言范式有关,这就是为什么需要它,所以请...向我解释一下。

    最佳答案

    另一种看待它的方法是将 () 可视化为元组为 0 的元组。

    鉴于 () 用于分隔元组,我们得到

    (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

    在基于 lambda 演算的函数式语言中,函数采用单个参数并返回单个值。柯里化(Currying)支持多个参数。参数和返回值也可以是元组以支持多个值。

    我对unit/()的解释,是没有值,表示为一个元组。

    关于f# - 如果F#支持void类型,为什么我需要使用单位类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704860/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com