简体   繁体   English

什么是:_ *以及它如何在Scala中实现?

[英]What is :_* and How it implement in Scala?

What i know: 我知道的:

scala> def fx(s: String *) = s.foreach(println)
fx: (s: String*)Unit

scala> val lst = List("1","2","3")
lst: List[java.lang.String] = List(1, 2, 3)

scala> fx(lst:_*)
1
2
3

What i want to know: 我想知道的是什么:

  1. How can I implement :_* ? 我该如何实现:_* by map? 按地图?
  2. Is there any other way that replace it? 有没有其他方法可以取代它?
  3. How :_* defined in Scala? 如何:_ *在Scala中定义?

Thank you 谢谢

It is only a syntactic sugar to indicates to the compiler that you already provide a sequence of elements, there is no other "implementation" of it. 向编译器指示你已经提供了一系列元素只是一个语法糖,没有其它的“实现”。 For more information, you can refer to the Scala Language Specification (§6.6, p. 78) 有关更多信息,请参阅Scala语言规范 (第6.6节,第78页)

For a method that takes variable arguments :_* means you want to pass the members of a collection as the variable arguments. 对于采用变量参数的方法:_*表示您希望将集合的成员作为变量参数传递。

The corresponding varargs example to yours above: 以上对应的varargs示例:

scala> fx("a", "b", "c")
a
b
c

You can't implement -- it is a language feature. 你无法实现 - 它是一种语言功能。 It doesn't mean anything outside the context of calling a varargs method. 它并不意味着在调用varargs方法之外的任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM