Scala: pass Seq to var-args functions

Scala

Scala Problem Overview


Given a function that takes a variable number of arguments, e.g.

def foo(os: String*) =
  println(os.toList)

How can I pass a sequence of arguments to the function? I would like to write:

val args = Seq("hi", "there")
foo(args)

Obviously, this does not work.

Scala Solutions


Solution 1 - Scala

foo(args:_*) does the trick. Instead of applying the sequence as one single argument, each element in the sequence will be used as an argument.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid CrawshawView Question on Stackoverflow
Solution 1 - ScalaJoa EbertView Answer on Stackoverflow