Why are default arguments not allowed in a Scala section with duplicate options?

According to Scala specifications Section 4.6.3 :

It is not possible to determine any default arguments in a parameter section with a repeating parameter.

In fact, if I define the following case class:

case class Example(value: Option[String] = None, otherValues: String*) 

The result that I get is expected as per 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 completely independent of the repeating argument, so why does this restriction occur?

+6
source share
1 answer

Because you can do this:

 case class Example(value: String = "default", otherValues: String*) 

And now, if you call Example("Hello", "world") , does the first "Hello" belong to value or otherValues ?

You can argue that the types in your examples are different, but the rules are getting too complicated to follow. For example, duplicate parameters are often used with type Any . This example case class Example(value: Option[String] = None, otherValues: Any*) has different types, but still struggles with the same problem

+6
source

All Articles