David Crawshaw
David Crawshaw

Reputation: 10577

Scala: pass Seq to var-args functions

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.

Upvotes: 79

Views: 39711

Answers (1)

Joa Ebert
Joa Ebert

Reputation: 6715

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.

Upvotes: 153

Related Questions