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 @ _
All the above answer looks great, but just need a sample to explain this . Here it is :
val x : Seq[Seq[Int]] = Seq(Seq(1),Seq(2))
def f(arg: Seq[Any]*) : Int = {
arg.length
}
f(x) //1 as x is taken as single arg
f(x:_*) // 2 as x is "unpacked" as a Seq[Any]*
So now we know what :_*
do is to tell compiler : please unpack this argument and bind those elements to the vararg parameter in function call rather than take the x as a single argument .
So in a nutshell, the :_*
is to remove ambiguity when pass argument to vararg parameter.