Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以为可变长度参数列表设置默认参数?
例子:
def foo(args: String*) = args.foreach(println)
如何设置默认参数args?
args
不,如果你尝试,编译器会告诉你:
错误:带有“*”参数的参数部分不允许有默认参数
但是您可以通过方法重载获得相同的结果:
class A { def foo(args: String*): Unit = args.foreach(println) def foo(): Unit = foo("A", "B", "C") }
这是您提供参数的时候:
scala> (new A).foo("A", "B") A B
这是“默认”:
scala> (new A).foo() A B C