djsecilla
djsecilla

Reputation: 410

Why are default arguments not allowed in a Scala section with repeated parameters?

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?

Upvotes: 8

Views: 1255

Answers (1)

Archeg
Archeg

Reputation: 8462

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

Upvotes: 11

Related Questions