1
  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

jilen
  • 5,633
  • 3
  • 35
  • 84

1 Answers1

5

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.

Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234