param: _* 在 Scala 中意味着什么?
作为 Scala (2.9.1) 的新手,我有一个 List[Event]
并希望将其复制到 Queue[Event]
中,但以下语法会产生一个Queue[List[Event]]
相反:
val eventQueue = Queue(events)
出于某种原因,以下内容有效:
val eventQueue = Queue(events : _*)
但我想了解它的作用以及为什么有效?我已经查看了 Queue.apply 函数的签名:
def apply[A](elems: A*)
我明白为什么第一次尝试不起作用,但第二次尝试的含义是什么?在本例中,:
和 _*
是什么,为什么 apply
函数不只采用 Iterable[A] ?
Being new to Scala (2.9.1), I have a List[Event]
and would like to copy it into a Queue[Event]
, but the following Syntax yields a Queue[List[Event]]
instead:
val eventQueue = Queue(events)
For some reason, the following works:
val eventQueue = Queue(events : _*)
But I would like to understand what it does, and why it works? I already looked at the signature of the Queue.apply
function:
def apply[A](elems: A*)
And I understand why the first attempt doesn't work, but what's the meaning of the second one? What is :
, and _*
in this case, and why doesn't the apply
function just take an Iterable[A]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
a: A
是类型归属;请参阅 Scala 中类型归属的目的是什么?: _*
是类型归属的一个特殊实例,它告诉编译器将序列类型的单个参数视为可变参数序列,即 varargs。使用
Queue.apply
创建一个具有单个序列或可迭代元素的Queue
是完全有效的,因此这正是当您给出单个 <代码>可迭代[A]。a: A
is type ascription; see What is the purpose of type ascriptions in Scala?: _*
is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence, i.e. varargs.It is completely valid to create a
Queue
usingQueue.apply
that has a single element which is a sequence or iterable, so this is exactly what happens when you give a singleIterable[A]
.这是一种特殊的表示法,告诉编译器将每个元素作为其自己的参数传递,而不是将所有元素作为单个参数传递。请参阅此处。
它是一个类型注释,指示序列参数,并在语言规范第 4.6.2 节“重复参数”中作为一般规则的“例外”提到。
当函数采用可变数量的参数时,它很有用,例如像
def sum(args: Int*)
这样的函数,可以作为sum(1)
调用、sum(1,2)
等。如果您有一个诸如xs = List(1,2,3)
之类的列表,则无法传递xs
本身,因为它是一个List
而不是Int
,但您可以使用sum(xs: _*)
传递其元素。This is a special notation that tells the compiler to pass each element as its own argument, rather than all of it as a single argument. See here.
It is a type annotation that indicates a sequence argument and is mentioned as an "exception" to the general rule in section 4.6.2 of the language spec, "Repeated Parameters".
It is useful when a function takes a variable number of arguments, e.g. a function such as
def sum(args: Int*)
, which can be invoked assum(1)
,sum(1,2)
etc. If you have a list such asxs = List(1,2,3)
, you can't passxs
itself, because it is aList
rather than anInt
, but you can pass its elements usingsum(xs: _*)
.对于 Python 人员:
Scala 的
_*
运算符或多或少相当于 Python 的 *-运算符。示例
从 链接由Luigi Plinge提供:
Python会看起来like:
并且都给出以下输出:
区别:解包位置参数
虽然 Python 的
*
运算符还可以处理位置参数/固定数量函数参数的解包:对 Scala 执行同样的操作:
将会失败:
但使用 scala 可以实现相同的效果:
根据 洛林·尼尔森这是它的工作原理:
进一步阅读:
For Python folks:
Scala's
_*
operator is more or less the equivalent of Python's *-operator.Example
Converting the scala example from the link provided by Luigi Plinge:
to Python would look like:
and both give the following output:
The Difference: unpacking positional parameters
While Python's
*
-operator can also deal with unpacking of positional parameters/parameters for fixed-arity functions:Doing the same with Scala:
will fail:
But it is possible to achieve the same with scala:
According to Lorrin Nelson this is how it works:
Futher reading: