According to Scala specs Section 4.6.3:
It is not allowed to define any default arguments in a parameter section with a repeated parameter.
In fact, if I define the following case class:
case class Example(value: Option[String] = None, otherValues: String*)
the result I get is the expected according to the specification:
error: a parameter section with a `*'-parameter is not allowed to have default arguments
case class Example(value: Option[String] = None, otherValues: String*)
But the question is why is this not allowed? The first argument of the class is totally independent from the repeated argument, so why is this restriction is place?
Because you could do this:
case class Example(value: String = "default", otherValues: String*)
And now if you call Example("Hello", "world")
, does the first "Hello"
belongs to value
or to otherValues
?
You could argue that the types in your examples are different, but the rules become too complicated to follow. For example repeated parameters often used with Any
type. This example case class Example(value: Option[String] = None, otherValues: Any*)
has different types, but still struggles with the same problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With