Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the equivalent to String... in scala

Tags:

java

scala

The title asks the question. Basically I am using a java library that takes a String... as a parameter. How can I call that in my scala code?

I'll add a bit more:

from scala code

def myScalaFunc(params:String*) {
   val myJavaClass = new JavaClass()
   myJavaClass(params)
}
like image 667
chiappone Avatar asked Dec 16 '22 02:12

chiappone


1 Answers

You have to expand the params into a series of arguments, not just a single collection argument. The easiest way to do this is by saying params: _*.

If the Java looks like:

public class VarargTest {
  public void javaFunc(String... args) { 
    // something
  }
}

Then the Scala caller looks like:

def scalaFunc(params: String*) = 
  (new VarargTest).javaFunc(params: _*)
like image 73
dhg Avatar answered Dec 18 '22 17:12

dhg