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
which is called as
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.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.