`:_*`(冒号下划线星)在 Scala 中有什么作用? [英] What does `:_*` (colon underscore star) do in Scala?

查看:139
本文介绍了`:_*`(冒号下划线星)在 Scala 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下来自 这个问题的代码:

def addChild(n: Node, newChild: Node) = n match {case Elem(prefix, label, attribs, scope, child @_*) =>Elem(前缀、标签、属性、范围、子级 ++ newChild : _*)案例_ =>error("只能给元素添加子元素!")}

里面的一切都很清楚,除了这一段:child ++ newChild : _*

它有什么作用?

我知道有 Seq[Node] 与另一个 Node 连接,然后呢?: _* 有什么作用?

解决方案

它破坏"1 序列.

看构造函数签名

new Elem(prefix: String, label: String, attributes: MetaData, scope: NamespaceBinding,孩子:节点*)

被称为

new Elem(prefix, label, attributes, scope,child1, child2, ... childN)

但这里只有一个序列,没有child1child2等,所以这允许将结果序列用作构造函数的输入.

快乐编码.

<小时>

1 这在 SLS 中没有一个可爱的名字,但这里有详细信息.重要的是,它改变了 Scala 将参数绑定到具有重复参数的方法的方式(如上面的 Node* 所示).

_* 类型注释在 SLS 的4.6.2 重复参数"中有介绍.

<块引用><块引用>

参数部分的最后一个值参数可以以*"为后缀,例如(..., x:T *).方法内这种重复参数的类型是序列类型 scala.Seq[T].带有重复参数 T * take 的方法可变数量的 T 类型参数.也就是说,如果一个方法 m 的类型(p1 : T1, . . , pn : Tn,ps : S*)U 应用于参数 (e1, . . . , ek) 其中 k >= n,然后m 在该应用程序中被认为具有类型 (p1 : T1, . . , pn : Tn,ps : S, . . , ps0S)U,类型 S 出现 k ¡ n 次,其中超出 ps 的任何参数名称是新鲜的.这条规则的唯一例外是如果最后一个参数被标记为通过 _* 类型注释的序列参数.如果将上述 m 应用于参数 (e1, . . . , en,e0 : _*),则该应用程序中 m 的类型被视为(p1 : T1, . . , pn : Tn,ps :scala.Seq[S])

I have the following piece of code from this question:

def addChild(n: Node, newChild: Node) = n match {
  case Elem(prefix, label, attribs, scope, child @ _*) => Elem(prefix, label, attribs, scope, child ++ newChild : _*)
  case _ => error("Can only add children to elements!")
}

Everything in it is pretty clear, except this piece: child ++ newChild : _*

What does it do?

I understand there is Seq[Node] concatenated with another Node, and then? What does : _* do?

解决方案

It "splats"1 the sequence.

Look at the constructor signature

new Elem(prefix: String, label: String, attributes: MetaData, scope: NamespaceBinding,
         child: Node*)

which is called as

new Elem(prefix, label, attributes, scope,
         child1, child2, ... childN)

but here there is only a sequence, not child1, child2, etc. so this allows the result sequence to be used as the input to the constructor.

Happy coding.


1 This doesn't have a cutesy-name in the SLS, but here are the details. The important thing to get is that it changes how Scala binds the arguments to the method with repeated parameters (as denoted with Node* above).

The _* type annotation is covered in "4.6.2 Repeated Parameters" of the SLS.

The last value parameter of a parameter section may be suffixed by "*", e.g. (..., x:T *). The type of such a repeated parameter inside the method is then the sequence type scala.Seq[T]. Methods with repeated parameters T * take a variable number of arguments of type T . That is, if a method m with type (p1 : T1, . . . , pn : Tn,ps : S*)U is applied to arguments (e1, . . . , ek) where k >= n, then m is taken in that application to have type (p1 : T1, . . . , pn : Tn,ps : S, . . . , ps0S)U, with k ¡ n occurrences of type S where any parameter names beyond ps are fresh. The only exception to this rule is if the last argument is marked to be a sequence argument via a _* type annotation. If m above is applied to arguments (e1, . . . , en,e0 : _*), then the type of m in that application is taken to be (p1 : T1, . . . , pn : Tn,ps :scala.Seq[S])

这篇关于`:_*`(冒号下划线星)在 Scala 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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