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

时间:2021-11-04 21:45:00

Why does an empty string with the string interpolation prefix fail to compile when followed by a statement?

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

For example

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

Fails to compile with error

无法编译时出错

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

But the following all compile fine:

但以下所有编译正常:


val test = s""

val test = ""
println("hello")

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

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

1 个解决方案

#1


It's a parser bug, fixed last January, so this should be ok in the latest 2.11.x releases.

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

Here's the bug report: https://issues.scala-lang.org/browse/SI-7919. The relevant comment (by Paul Phillips) is:

这是错误报告: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.

在完全空插值字符串后丢失换行符号。如果它是“1”,或分号而不是换行符,或两个换行符,或空字符串后面的注释,则编译。

class A {
  s""
  5
}

Here's the PR that fixed it: https://github.com/scala/scala/pull/3411

这是解决它的PR:https://github.com/scala/scala/pull/3411

#1


It's a parser bug, fixed last January, so this should be ok in the latest 2.11.x releases.

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

Here's the bug report: https://issues.scala-lang.org/browse/SI-7919. The relevant comment (by Paul Phillips) is:

这是错误报告: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.

在完全空插值字符串后丢失换行符号。如果它是“1”,或分号而不是换行符,或两个换行符,或空字符串后面的注释,则编译。

class A {
  s""
  5
}

Here's the PR that fixed it: https://github.com/scala/scala/pull/3411

这是解决它的PR:https://github.com/scala/scala/pull/3411