Which is equivalent to String ... in scala

The title asks the question. I mainly use a java library that takes a String ... parameter as a parameter. How can I call this in my scala code?

I will add a little more:

from scala code

def myScalaFunc(params:String*) { val myJavaClass = new JavaClass() myJavaClass(params) } 
+4
source share
3 answers

You need to expand params into a series of arguments, not just one collection argument. The easiest way to do this is to say params: _* .

If Java looks like this:

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

Then the calling Scala looks like this:

 def scalaFunc(params: String*) = (new VarargTest).javaFunc(params: _*) 
+6
source

If the method is defined:

 void method(String... param) 

Then either call it like this:

 method("String 1", "String 2") 

Or deploy Seq using this special syntax:

 method(Seq("String 1", "String 2"): _*) 

Given your sample code (I assume my edit is correct):

 myJavaClass.myJavaMethod(params: _*) 
+5
source

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 as follows:

 def method (param:String*) 
+1
source

Source: https://habr.com/ru/post/1402728/


All Articles