I wrote a class that takes varargs as a parameter and sets it to default so that the user can often create it without specifying a parameter:
class MyClass(values: Int* = 42) { }
However, the compiler and 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: (this is very ambiguous).
class MyClass(value: Int = 42, otherValues: Int*) { }
I wonder why it is not allowed to use the default varargs parameter. What is the reason or technical reason? (My assumption is that specifying empty varargs will require some special syntax or idiom, but I'm not sure if this is sufficient reason.)
source share