Search code examples
arraysstringscalavariadic-functions

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


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 ?


Solution

  • 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".