tags:

views:

131

answers:

1

In Scala (2.7), if I have this function:

def foo(args: Array[String]) =
  for (arg <- args) println(arg)

If I now try to define the following:

def bar(args: String*) = foo(args)

then the compiler complains:

<console>:5: error: type mismatch;
 found   : String*
 required: Array[String]
       def bar(args: String*) = foo(args)
                                ^

I don't understand this error, since the Programming Scala book states that the type of args inside function bar is actually Array[String]. How am I supposed to write such a wrapper function with repeated arguments?

+13  A: 
scala> def foo(args: Array[String]) = for(arg <- args) println(arg)
foo: (args: Array[String])Unit

scala> def bar(args: String*) = foo(args.toArray)
bar: (args: String*)Unit

scala> bar("hello", "world")
hello
world

You need to perform above conversion because varargs in Scala are implemented as Seq, not Array.


Here is how varargs are usually forwarded in Scala:

scala> def fooV(args: String*) = args foreach println
fooV: (args: String*)Unit

scala> def fooS(args: Seq[String]) = fooV(args: _*)
fooS: (args: Seq[String])Unit

scala> def bar(args: String*) = fooV(args: _*)
bar: (args: String*)Unit

scala> def barS(args: Seq[String]) = args foreach println
barS: (args: Seq[String])Unit

scala> def barV(args: String*) = barS(args)
barV: (args: String*)Unit

scala> def barV(args: String*) = barS(args.toSeq)
barV: (args: String*)Unit
missingfaktor
I completely missed the problem the first time. Thanks S.O. for making correct answers float up!
Daniel