开发者

Scala: pass Seq to var-args functions

开发者 https://www.devze.com 2022-12-13 04:55 出处:网络
Given a function that takes a variable number of arguments, e.g. def foo(os: String*) = println(os.toList)

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开发者_StackOverflow would like to write:

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

Obviously, this does not work.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消