Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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?

like image 636
djsecilla Avatar asked Nov 19 '15 16:11

djsecilla


1 Answers

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

like image 195
Archeg Avatar answered Nov 03 '22 01:11

Archeg