Reputation: 429
I have a piece of Scala code:
class A {
def main(args: Array[String])
}
Then a piece of Java code:
class B {
public static int test(final String... args) {
System.out.println("This is a test.");
// Use the arguments
...
}
}
What I want is to call B.test within A.main like below:
class A {
def main(args: Array[String]) = {
val result = B.test(args)
}
}
The above code won't work because the types do not match. If somebody could provide an elegant solution it would be highly appreciated.
Please only change the Scala part as the Java part will come from another lib. Thank you!
Upvotes: 1
Views: 1126
Reputation: 8378
As per section 4.6.2 of scala language specification, to feed a list/array to the function with repeated parameters (T... args)
in case with java, or (args: T*)
with scala, the argument should be marked to be a sequence argument via a _*
type annotation.
In this particular case B.test(args: _*)
will work.
Upvotes: 2