Home > Enterprise >  How to pass the values of array of string as repeated parameter (varargs) of String in scala
How to pass the values of array of string as repeated parameter (varargs) of String in scala

Time:11-01

I am new to scala, trying to figure out a way to pass the values of array of string as repeated parameter of String in scala. There is a method which accepts (String,String*) as arguments. I have array that has the values that i need to pass to the above method, how can i do that ?

CodePudding user response:

Scala requires that you explicitly mark the argument as a variadic argument.

myMethod(firstArg, arrayArg: _*)

The : _*, although it looks like a type annotation, is actually a special bit of syntax you use when you call the method. It says "the thing to my left is an array, and you should pass it (and only it) as the whole variadic argument".

CodePudding user response:

Scala 3 version:

    val arr = Array("b", "c")
    test("a", arr*)
  • Related