Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala string interpolation empty string compile error

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")

like image 833
Bobby Avatar asked Mar 17 '23 04:03

Bobby


1 Answers

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

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

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
}

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

like image 136
Gabriele Petronella Avatar answered Mar 19 '23 03:03

Gabriele Petronella