Here is something I can do in java, take the results of a repeated parameter and pass it to another method:
public void foo(String ... args){bar(args);}
public void bar(String ... args){System.out.println("count="+args.length);}
In scala it would look like this:
def foo(args:String*) = bar(args)
def bar(args:String*) = println("count="+args.length)
But this won’t compile, the bar signature expects a series of individual strings, and the args passed in is some non-string structure.
For now I’m just passing around arrays. It would be very nice to use starred parameters. Is there some way to do it?
Java makes an assumption that you want to automatically convert the Array
argsto varargs, but this can be problematic with methods that accept varargs of type Object.Scala requires that you be explicit, by ascribing the argument with
: _*.You can use
: _*on any subtype ofSeq, or on anything implicitly convertable to aSeq, notablyArray.It is explained well with examples in the Language Reference, in section 4.6.2.
Note that these examples are made with Scala 2.8.0.RC2.