Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: pass Seq to var-args functions

Tags:

scala

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.

like image 671
David Crawshaw Avatar asked Dec 02 '09 10:12

David Crawshaw


1 Answers

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.

like image 141
Joa Ebert Avatar answered Sep 18 '22 23:09

Joa Ebert