void 与 Unit 对比

发布于 2024-08-14 21:11:00 字数 107 浏览 10 评论 0原文

我想了解这两个编程概念之间的区别。第一个表示不存在数据类型,而后者表示类型存在但没有信息。此外,我认识到 Unit 来自函数式编程理论基础,但我仍然无法理解单元原语的可用性(例如,在 F# 程序中)。

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 but there is no information. Additionally, I recognize that Unit comes from functional programming theoretical foundation but I still cannot understand what is the usability of the unit primitive (e.g., in an F# program).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-08-21 21:11:00

在函数式编程中,我们通常谈论将输入映射到输出。从字面上看,这意味着将参数映射到其返回值。但如果某个东西要成为数学/范畴论意义上的函数,它就必须返回某物void 值表示函数不返回任何内容,这在这些术语中是无意义的。

unit 是对 void 的功能性回答。它本质上是一种只有一个值的类型,()。它有多种用途,但这里有一个简单的用途。假设您在更传统的命令式语言中有类似的内容:

public static <T, U> List<U> map(List<T> in, Function<T, U> func) {
  List<U> out = new ArrayList<U>(in.size());
  for (T t : in) {
     out.add(func.apply(t));
  }
  return out;
}

这将特定的函数 func 应用于列表中的每个元素,生成 func 输出的新列表类型。 但是如果您传入一个仅打印其参数的函数,会发生什么?它没有输出类型,那么您可以为U输入什么?

在某些语言中,传入这样的函数会破坏此代码(例如在 C# 中,您不能将 void 分配给泛型类型)。您必须诉诸解决方法,例如使用 Action,这可能会变得笨拙。

这就是单位概念的用处:一种类型,但只能采用单个值。这极大地简化了链接和组合等事情,并大大减少了您需要担心的特殊情况的数量。

In functional programming, we usually speak of mapping inputs to outputs. This literally means mapping an argument to its return value(s). But if something is going to be a function in the mathematical/category-theory sense, it has to return something. A void value represents that a function returns nothing, which is nonsensical in these terms.

unit is the functional answer to void. It's essentially a type with only one value, (). It has a number of uses, but here's a simple one. Let's say you had something like this in a more traditional imperative language:

public static <T, U> List<U> map(List<T> in, Function<T, U> func) {
  List<U> out = new ArrayList<U>(in.size());
  for (T t : in) {
     out.add(func.apply(t));
  }
  return out;
}

This applies a particular function func to every element on the list, producing a new list of func's output type. But what happens if you pass in a Function that just prints its arguments? It won't have an output type, so what can you put for U?

In some languages, passing in such a function would break this code (like in C#, where you can't assign void to a generic type). You'd have to resort to workarounds like having an Action<T>, which can get clunky.

This is where the concept of unit is useful: it is a type, but one that may only take on a single value. That greatly simplifies things like chaining and composition, and vastly reduces the number of special cases you have to worry about.

我们的影子 2024-08-21 21:11:00

单位类型只是让一切变得更加规则。在某种程度上,您可以将 F# 中的每个函数视为采用单个参数并返回单个结果。不需要任何参数的函数实际上将“unit”作为参数,而不返回任何结果的函数则返回“unit”作为结果。这有多种优点;首先,考虑一下在 C# 中如何需要大量“Func”委托来表示返回值的各种参数的函数,以及大量不返回值的“Action”委托(因为例如 Func<; int,void> 不合法 - void 不能以这种方式使用,因为它不完全是“真实”类型)。

另请参阅 F# 函数类型:元组和柯里化的乐趣

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<int,void> 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文