jilen
jilen

Reputation: 5763

scala varargs (Any*) wrapped twice

  def main(args: Array[String]) {
      foo("hello")
  }

  def foo(args:Any*){
    bar(args)
  }

  def bar(args:Any *){
    println(args)
  }

Look the code above, the output is WrappedArray(WrappedArray(hello))

The String 'hello' is wrapped twice, how to avoid this

Upvotes: 1

Views: 443

Answers (1)

Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 59994

In the invocation of bar, write this:

bar(args: _*)

This tells the compiler to use the args in args, which at this point is very similar to a Seq[T], and pass each of them separately to bar, instead of considering args as the first of the repeated parameters that bar accepts.

Upvotes: 5

Related Questions