1

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!

5
  • 4
    I don't have the repl at hand to verify, but think that B.test(args:_*) should work. Commented Jan 10, 2014 at 10:28
  • @folone Yup, that's it! Commented Jan 10, 2014 at 10:44
  • I have just tested and it worked. Thank you! Commented Jan 10, 2014 at 10:44
  • @folone You should post that as an answer, so the question can be closed Commented Jan 10, 2014 at 15:13
  • @0__ Right, did that. Commented Jan 13, 2014 at 13:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.