Say I have a function
def f(a:Int = 0, b:String = "", c:Float=0.0, foos: Foo*) { ... }
Notice the use of default arguments for some parameters. Typically, to make use of default values, you invoke a function with named parameters like so:
val foo = Foo("Foo!")
f(foos = foo)
This syntax works because I’m only invoking the method with one foo. However, if I have two or more
val foo1 = Foo("Foo!")
val foo2 = Foo("Bar!")
f(foos = ...)
it’s not so obvious what should be fed in here. Seq(foo1,foo2) and Seq(foo1,foo2):_* do not type check.
What’s more, how can I call it with no foos?
// All out of foos!
f(foos = ...)
For this case, calling the function with empty parentheses (f()) does not work.
Thanks!
Given the Scala 2.9 limitations that Paolo mentioned, you can still use currying to divide the parameters in different sections, one which uses named parameters with default arguments, and one for varargs (or multiple curried argument sections if you want more than one vararg parameter). Regarding readability the result is imho almost better than using named arguments only: