Scala:将 Seq 传递给 var-args 函数 [英] Scala: pass Seq to var-args functions
本文介绍了Scala:将 Seq 传递给 var-args 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个接受可变数量参数的函数,例如
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)
显然,这是行不通的.
推荐答案
foo(args:_*)
可以解决问题.序列中的每个元素都将用作一个参数,而不是将序列作为单个参数应用.
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.
这篇关于Scala:将 Seq 传递给 var-args 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文