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.

2 Answers2

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!

Steve Waldman
  • 13,689
  • 1
  • 35
  • 45
2

This should work:

javaMethod(args: _*)

Example:

val args = Array("1", "2")
String.format("%s %s", args: _*)
senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32