Hi,
The arguments of the following functions use varargs that its type is parameterized types:
def f[A, AF](a: (A*) => AF) = ???
def f[A, AF]: (A*) => AF = ???
The above codes cannot be compiled by Scala 3.3.0
How to solve it?
Thanks for your help.
Guofeng
Varargs are just syntactic sugar for a Seq
parameter, i.e. a function taking A*
and returning AF
would have the type Seq[A] => AF
. Specifying the type as varargs is not supported. This means you can’t call a lambda function with variable arguments, but instead of a(arg1, arg2, arg3)
you’ll have to write a(Seq(arg1, arg2, arg3))
inside your method f
.
You can pass a varargs method when a function taking a Seq is expected, the following is valid:
def f[A, AF](a: Seq[A] => AF) = ???
def varargMethod(ints: Int*): String = ???
f(varargMethod)
1 Like
The message is clearer in Scala 2.
Welcome to Scala 2.13.11 (OpenJDK 64-Bit Server VM, Java 20.0.1).
Type in expressions for evaluation. Or try :help.
scala> def f[A, AF](a: (A*) => AF) = ???
^
error: repeated parameters are only allowed in method signatures; use Seq instead
compare
Welcome to Scala 3.3.0 (20.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> def f[A, AF](a: (A*) => AF) = ???
-- [E040] Syntax Error: ------------------------------------------------------------------------------------------------
1 |def f[A, AF](a: (A*) => AF) = ???
| ^
| ')' expected, but identifier found
I’d like a worksheet or REPL that shows me results from both compilers.
Edit:
The issue for eta-expansion of method taking varargs was this ticket. The lengthy discussions about what ought to be supported or disallowed make for interesting archaeology, a long answer to the OP. It is ten years since -Yeta-expand-keeps-star
(in 2.10) was deprecated for 2.11.
The paulpism from a related ticket that is worth preserving, to capture the expectation that discussing a point must lead to clarity:
spontaneous disambiguation
adriaanm’s coinage for the OP example:
repeat offenders