1

I'm converting some of the Elasticsearch Java API to Scala. There are some methods in Java that take a variable of type String... indices, so I'm trying to provide a Scala function with a variable of type indices: String*, that encapsulates that Java method. I didn't find a way to convert String* to String....

I appreciate your help.

0

2 Answers 2

6

Scala interoperates with Java vararg functions, so if you knew the arguments, you could just provide them.

But the Scala String* will come through as a Scala Seq. But you'll want the contents of that Seq to be unrolled and provided as arguments to the Java function.

Scala has a special syntax for specifying that the contents of a Seq should be unrolled and passed along to a function. You write : _* after the name of the Seq.

So, it should be something like this:

def myScalaFunction( args : String* ) : Unit = {
   javaStringVaragsFunction( args : _* )
}

Give it a try!

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

Comments

2

This should work:

javaMethod(args: _*)

Example:

val args = Array("1", "2")
String.format("%s %s", args: _*)

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.