三个点是否包含多种含义? [英] Do three dots contain multiple meanings?

查看:71
本文介绍了三个点是否包含多种含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,"..."表示下面代码段中数组的长度.

As I recognize, "..." means the length of the array in the below snippet.

var days := [...]string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } 

另一方面,正如我猜想的那样,"..."表示将片段 y 解压缩为以下代码段中int的参数.我对此不太确定.

On the other hand, "..." means unpacking the slice y to arguments of int in the below snippet, as I guess. I'm not really sure about this.

x := []int{1,2,3}
y := []int{4,5,6}
x = append(x, y...)

现在,这两种含义的差异使我很难理解"..."是什么.

Now, the difference in the two meanings makes it hard for me to understand what "..." is.

推荐答案

您已经在Go中注意到了 ... 的两种情况.实际上,这里有3:

You've noted two cases of ... in Go. In fact, there are 3:

[...]int{1,2,3}

在编译时将评估为[3] int {1,2,3}

Evaluates at compile time to [3]int{1,2,3}

a := make([]int, 500)
SomeVariadicFunc(a...)

解压缩 a 作为函数的参数.这与您错过的一个(可变参数的定义)相符:

Unpacks a as the arguments to a function. This matches the one you missed, the variadic definition:

func SomeVariadicFunc(a ...int)

现在还有一个问题(摘自OP上的评论)-为什么在所有这些情况下...都可以在语义上起作用?答案是英语(和其他语言),这是被称为省略号.从那篇文章中

Now the further question (from the comments on the OP) -- why can ... work semantically in all these cases? The answer is that in English (and other languages), this is known as an ellipsis. From that article

省略号(复数椭圆;来自古希腊语:ἔλλειψις,élleipsis,遗漏"或落空")是一系列点通常表示故意遗漏单词,句子或整个部分,无需更改其原始含义. 1 根据其上下文和在句子中的位置,省略号可以也表示未完成的想法,重要的陈述,轻微的停顿一下,保持紧张或尴尬的沉默.

Ellipsis (plural ellipses; from the Ancient Greek: ἔλλειψις, élleipsis, "omission" or "falling short") is a series of dots that usually indicates an intentional omission of a word, sentence, or whole section from a text without altering its original meaning.1 Depending on their context and placement in a sentence, ellipses can also indicate an unfinished thought, a leading statement, a slight pause, and a nervous or awkward silence.

在数组的情况下,这与省略单词,句子或整个部分"的定义匹配.您将省略数组的大小,而让编译器为您解决.

In the array case, this matches the "omission of a word, sentence, or whole section" definition. You're omitting the size of the array and letting the compiler figure it out for you.

在可变参数情况下,其含义相同,但含义不同.它还暗示着未完成的思想".我们经常用"..."来表示等等".我要买面包,鸡蛋,牛奶..."在这种情况下,"..."表示其他与面包,鸡蛋和牛奶相似的东西".在 append 中的使用表示此列表的一个元素,以及所有其他元素".这也许不是那么直接的直观用法,但是对于母语为母语的人来说,这很有意义. a [0] ... 甚至可能是 a [0],a [1],a [2] ... ,但这会导致空片(与 ... 语法一起使用)的明显问题,更不用说冗长了.

In the variadic cases, it uses the same meaning, but differently. It also has hints of "an unfinished thought". We often use "..." to mean "and so on." "I'm going to get bread, eggs, milk..." in this case "..." signifies "other things similar to breads, eggs, and milk". The use in, e.g., append means "an element of this list, and all the others." This is perhaps the less immediately intuitive usage, but to a native speaker, it makes sense. Perhaps a more "linguistically pure" construction would have been a[0]... or even a[0], a[1], a[2]... but that would cause obvious problems with empty slices (which do work with the ... syntax), not to mention being verbose.

通常,"..."用于表示许多事物",因此,这两种用法都有意义.许多数组元素,许多切片元素(尽管一个是创建,而另一个是调用).

In general, "..." is used to signify "many things", and in this way both uses of it make sense. Many array elements, many slice elements (albeit one is creation, and the other is calling).

我想隐藏的问题是这是一种好的语言设计吗?"一方面,一旦您了解了语法,对于大多数以英语为母语的人来说,它就非常有意义,因此在某种意义上说它是成功的.另一方面,以这种方式不重载符号也很有价值.我可能会选择其他符号来进行数组拆包,但是我不能责怪他们使用对语言设计人员来说可能很直观的符号.尤其是因为阵列版本甚至不经常使用.

I suppose the hidden question is "is this good language design?" On one hand, once you know the syntax, it makes perfect sense to most native speakers of English, so in that sense it's successful. On the other hand, there's value in not overloading symbols in this way. I probably would have chose a different symbol for array unpacking, but I can't fault them for using a symbol that was probably intuitive to the language designers. Especially since the array version isn't even used terribly often.

如前所述,这对于编译器没有问题,因为情况永远不会重叠.您永远不会让 [...] 也意味着解开此包",因此不会出现符号冲突.

As mentioned, this is of no issue to the compiler, because the cases can never overlap. You can never have [...] also mean "unpack this", so there's no symbol conflict.

(此外:Go中还有另一种用法,我省略了,因为它不是语言本身,而是构建工具.键入类似 go test ./...之类的东西表示测试这个包,以及这个包的子目录中的所有包."但是我对其他用法的解释应该很清楚,为什么在这里有意义.)

(Aside: There is another use of it in Go I omitted, because it's not in the language itself, but the build tool. Typing something like go test ./... means "test this package, and all packages in subdirectories of this one". But it should be pretty clear with my explanation of the other uses why it makes sense here.)

这篇关于三个点是否包含多种含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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