Scala: Why can't a method have multiple vararg arguments?

Can someone tell me why this restriction exists? Is this related to the JVM or the Scala compiler?

$ scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def toText(ints: Int*, strings: String*) = ints.mkString("") + strings.mkString("")
<console>:7: error: *-parameter must come last
       def toText(ints: Int*, strings: String*) = ints.mkString("") + strings.mkString("")
+4
source share
2 answers

Scala is based on the Java virtual machine, which is configured to accept varargs only as the last argument, only one argument for each method. This does not work, so the compiler works.

To imagine this in perspective, imagine this method signature:

someMethod(strings1: String*, strings2: String*)

Let's say you skip 4 separate lines when called. The compiler does not know which String object vararg belongs to.

+5
source

Scala varargs, ( ):

scala> def toText(ints: Int*)(strings: String*) = 
         ints.mkString("") + strings.mkString("")

scala> toText(1,2,3)("a", "b")

res0: String = 123ab

. varargs - , ( , , ?).

, , ( ), , JVM, varargs .

, , , .

. Java- .

+12

All Articles