做三个点(称为通配符?)包含多重含义? [英] Do three dots (which is called wildcard?) contain multiple meanings?

查看:96
本文介绍了做三个点(称为通配符?)包含多重含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var days:

= [...]字符串{Sun,Mon,Tue,Wed,Thu,Fri,Sat}
y 解压缩到下面的int参数中就像我猜想的那样。我不太确定这一点。

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

现在,这两个意思的区别让我很难理解...是什么。

任何帮助都将被感激。


<您在Go中注意到两个 ... 的情况。实际上,有3个:

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

在编译时计算 为[3] int {1,2,3}

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

解包 a 作为函数的参数。这与您错过的变量定义匹配:

  func SomeVariadicFunc(a ... int)

code>

现在进一步的问题(来自OP的评论) - 为什么可以......在所有这些语义上工作案例?答案是,在英语(和其他语言)中,这被称为省略号。从那篇文章


省略号(复数椭圆;来自古希腊文:ἔλλειψις,
élleipsis,遗漏或不足 )是一系列的点,b $ b通常表示在不改变其原始含义的情况下故意省略单词,句子或
的整个部分。 wikipedia.org/wiki/Ellipsisrel =noreferrer> 1
根据句子的上下文和放置位置,省略号可以表示未完成的想法,领先声明,轻微的
停顿,以及一种紧张或尴尬的沉默。


在数组情况下,它匹配 ,句子或整个部分的定义。您省略了数组的大小,并让编译器为您找出它。



在可变参数的情况下,它使用相同的含义,但使用不同的含义。它也有未完成的想法的暗示。我们经常使用...来表示等等。 我要拿面包,鸡蛋,牛奶......在这种情况下,......表示其他类似面包,鸡蛋和牛奶的东西。例如 append 中的用法表示该列表的一个元素,以及所有其他元素。这可能不太直观,但对于母语人士来说,这是有道理的。也许更纯粹的语言的结构会是 a [0] ... 甚至 a [0],a [1],a [2] ... 但这会导致空切片(这对 ... 语法有效)有明显的问题,更不用说了一般来说,...用来表示很多事情,这样,它的两种用法都是有道理的。许多数组元素,很多切片元素(尽管一个是创建,另一个是调用)。



我想隐藏的问题是这是好的语言设计吗?一方面,一旦你知道语法,对于大多数母语为英语的人来说,这是非常有意义的,所以从这个意义上说,它是成功的。另一方面,以这种方式不会超载符号是有价值的。我可能会选择一个不同的符号来进行数组拆包,但是我不能因为使用对语言设计师来说很直观的符号而错误。特别是因为数组版本甚至不经常使用。



如前所述,这对编译器没有任何问题,因为这些情况永远不会重叠。你永远不能有 [...] 也意味着解开这个,所以没有符号冲突。






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


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

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

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.
Any help would be appreciate.

解决方案

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

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

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

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

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

func SomeVariadicFunc(a ...int)

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

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.

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.


(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天全站免登陆