Scala字符串插值空字符串编译报错

Scala string interpolation empty string compile error

为什么带有字符串插值前缀的空字符串在后跟语句时编译失败?

例如

val test = s""
println("hello")

编译失败

error: value println is not a member of String
possible cause: maybe a semicolon is missing before `value println'?
       println("hello")

但是下面的都可以正常编译:


val test = s""

val test = ""
println("hello")

def test() { s"" }
println("hello")

val test = s" "
println("hello")

这是一个解析器错误,已于去年 1 月修复,因此在最新的 2.11.x 版本中应该没问题。

这是错误报告:https://issues.scala-lang.org/browse/SI-7919。 相关评论(作者:Paul Phillips)是:

It's losing the newline token after a completely empty interpolated string. If it's s"1", or a semicolon instead of a newline, or two newlines, or a comment after the empty string, it compiles.

class A {
  s""
  5
}

这是修复它的 PR:https://github.com/scala/scala/pull/3411