Reputation: 2908
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)
}
Upvotes: 4
Views: 569
Reputation: 52681
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: _*)
Upvotes: 6
Reputation: 2832
If the method is defined:
void method(String... param)
Then either call it like this:
method("String 1", "String 2")
Or expand a Seq using this special syntax:
method(Seq("String 1", "String 2"): _*)
Given your example code (I'm assuming my edit is correct):
myJavaClass.myJavaMethod(params: _*)
Upvotes: 5
Reputation: 236024
In Java, this is the syntax for a method that receives a variable number of arguments:
void method(String... param)
In Scala, the equivalent syntax is this:
def method (param:String*)
Upvotes: 1