Passing elements of a List as parameters to a function with variable arguments

Take a look at this example, I will define a function printme that takes vargs of type String

def printme(s: String*) = s.foreach(println)


scala> printme(List("a","b","c"))

<console>:9: error: type mismatch;
 found   : List[String]
 required: String
              printme(List(a,b,c))

What you really need to un-pack the list into arguments with the :_* operator

scala> val mylist = List("1","2","3")
scala> printme(mylist:_*)
1
2
3

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)