在榆树中,空括号`()`是什么意思? [英] What do the empty parentheses `()` mean in Elm?

查看:223
本文介绍了在榆树中,空括号`()`是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现它们是一个空的元组.但是,它们是否也被Elm程序员用作约定,以表示值可以忽略"?

I found out they are mean an empty tuple. However, are they also used as a convention by Elm programmers to mean "value can be ignored"?

is13 : Int -> Result String ()
is13 code =
  if code == 13 then Ok () else Err "not the right key code"

来源: https://github.com/pdamoc/elmChallenges/blob/master/challenge5.elm

推荐答案

空括号()是所谓的 unit 类型,即只能有一个价值.具有至少一项的元组类型可以具有任意数量的值(例如,定义为一个Int的元组的类型可以具有无限数量的值,从(-∞)(+∞)).但是空元组"有多少个可能的值?只是一个,因此为什么叫它 unit 类型.

The empty parentheses () are what's known as a unit type, that is, a type that can only ever have a single value. A tuple type with at least one item can have any number of values (for example, a type defined as a tuple of one Int could have an infinite number of values, from (-∞) to (+∞)). But how many possible values of "the empty tuple" are there? Just one, hence why it's called a unit type.

单位类型的值是您可以在其他语言具有nullvoid类型的地方使用它,同时避免null附带的问题.例如,您已经注意到,它经常用在您想说我不在乎值是什么"的地方. 但是那里有一个价值;不是缺少值.

The value of a unit type is that you can use it in places where other languages would have a null or void type, while avoiding the problems that null carries with it. For example, as you've noticed, it's often used in places where you want to say "I don't care what the value is". But there is a value there; it's not the absence of a value.

这有很多优点;例如,您可以说" ALL 函数返回一个值",这就是事实.有时,该值是您不关心的值,但是所有函数将返回一个值.在void类型的语言中,您不能这么说.例如,C#有两种不同的方法来声明函数委托:Func<T>表示返回类型T的函数,而Action表示返回void的函数.在Elm(以及F#和使用单元类型的其他功能语言)中,不需要进行区分:所有函数都返回一个值,因此所有函数都可以视为与Func<T>等效.有时,类型T是空的元组,但这意味着您不必编写两个版本的map.

This has many advantages; for example, you can say "ALL functions return a value", and this will be true. Sometimes the value is one you don't care about, but all functions will return a value. In languages with a void type, you can't say that. For example, C# has two different ways to declare a function delegate: Func<T> for a function returning type T, and Action for a function returning void. In Elm (and F#, and other functional languages that use a unit type), there's no need for that distinction: all functions return a value, so all functions could be treated as the equivalent of Func<T>. Sometimes type T is the empty tuple, but it means that you don't have to write two versions of, say, map.

另一个优点是,它使您可以更轻松地编写函数.这是从有关单元类型的Wikipedia页面中获取的示例.在C语言中,您无法执行以下操作:

Another advantage is that it lets you compose functions more easily. Here's an example taken from the Wikipedia page on unit types. In C, you can't do this:

void f(void) {}
void g(void) {}

int main(void)
{
  f(g()); // compile-time error here
  return 0;
}

这是一个错误,因为不允许您将void作为参数传递:这是值的 .这意味着您不能使用标准功能组合将功能fg链接在一起.但是这是同一篇文章的C ++的另一段代码,展示了如果给C ++一个类似Elm的单元类型,它将会像什么:

This is an error, because you're not allowed to pass void as a parameter: it's the absence of a value. Which means that you can't use standard function composition to chain functions f and g together. But here's another snippet from the same article, of C++ this time, showing what it would look like if you gave C++ an Elm-like unit type:

class unit_type {};
const unit_type the_unit;

unit_type f(unit_type) { return the_unit; }
unit_type g(unit_type) { return the_unit; }

int main()
{
  f(g(the_unit));
  return 0;
}

现在,由于g返回一个实际值(即使它是您不关心的值),因此您可以有意义地编写fg.从长远来看,这使您的程序更优雅,更易于阅读.

Now, because g returns a real value (even though it's one you don't care about), you can compose f and g meaningfully. This makes your program more elegant and easy to read in the long run.

一些可能对您有帮助的进一步阅读:

Some further reading that might help you:

  1. https://en.wikipedia.org/wiki/Unit_type (我已经已经在此答案中链接一次)
  2. 如果F#支持void类型,为什么我需要在F#中使用单元类型?
  1. https://en.wikipedia.org/wiki/Unit_type (which I've already linked once in this answer)
  2. Why do I need to use the unit type in F# if it supports the void type?

希望这会有所帮助!

这篇关于在榆树中,空括号`()`是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆