I wrote a class which accepts a varargs as a parameter, and specify its default so that a user can often instantiate it without specifying a parameter:
class MyClass(values: Int* = 42) { }
However, the compiler and the REPL give me the following errors:
<console>:7: error: type mismatch;
found : Int(42)
required: Int*
class MyClass(values: Int* = 42) { }
^
<console>:7: error: a parameter section with a `*'-parameter is not allowed to have default arguments
class MyClass(values: Int* = 42) { }
As a workaround, I tried the following, but it didn't work either: (it's very ambiguous obviously.)
class MyClass(value: Int = 42, otherValues: Int*) { }
I wonder why it is unallowed to have the default for a varargs parameter. What is the reasoning or technical reason lying under here? (My guess is that specifying an empty varargs would require some special syntax or idiom, but I'm not sure if this is an enough reason.)
def foo(result: Int = 0, xs: String*) = ???
is not acceptable then? It is perfectly feasible for the compiler to recognizefoo(0, "impossible to compile")
as a legal construction. – Krasnoyarsk