Closed som-snytt closed 2 years ago
This is not allowed in Scala 2. It fails with the error repeated argument not allowed here
.
Given that we can do
extension (sc: StringContext) def sum(xs: Seq[Int]): String = xs.sum.toString
def test(xs: List[Int]) = sum"${ xs }"
but not
extension (sc: StringContext) def sum(xs: Int*): String = xs.sum.toString
def test = sum"${ 1, 2, 3 }"
it seems natural to not allow the variable arguments in string interpolators.
We need a better error message
extension (sc: StringContext) def sum(xs: Int*): String = xs.sum.toString
def test1(xs: List[Int]) = sum"${ xs* }" // needs fix in desugar
def test2(xs: List[Int]) = sum"${ xs: _* }" // needs fix in parser
See
The test on Scala 2 is here
I noticed the behavior backporting Scala 3 messages.
I think you mean the macro expansion of s
is broken on Scala 2.
scala> implicit class summer(val sc: StringContext) { def sum(xs: Int*) = xs.mkString("+") + "=" + xs.sum.toString }
class summer
scala> sum"${42}${17}"
val res0: String = 42+17=59
scala> sum"${List(42,27): _*}"
val res1: String = 42+27=69
scala> s"${List(42,27): _*}"
^
error: no `: _*` annotation allowed here
(such annotations are only allowed in arguments to *-parameters)
// val res1: String = ("".+((scala.`package`.List.apply[Int](42, 27): _*)): String);
Strange. I tried the same example in Scastie for Scala 2.13.8 and it failed
implicit class StringContextOps(val sc: StringContext) extends AnyVal {
def sum(xs: Int*): String = xs.sum.toString
}
def test = sum"${List(42,27): _*}" // error: repeated argument not allowed here
See https://scastie.scala-lang.org/KP2jZBi0TkmVEA24VeOUTQ
Could it be a bug in the Scala 2 repl?
In Scala 3 the repl parser is consistent with the normal parser.
@nicolasstucki that is a ScalametaParser
message
./scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala: syntaxError("repeated argument not allowed here", at = arg.tokens.last.prev)
Compiler version
3.1.1
Minimized code
Output
DNC
Expectation