Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a bug in Scala 2.10 String Interpolation inside a multiline String with backslash?

Using Scala 2.10.0-RC1, I tried to use String Interpolation inside a Windows file path, e.g. like this:

val path = s"""c:\foo\bar\$fileName.csv""" 

And got an Exception

java.lang.StringIndexOutOfBoundsException: String index out of range: 11

Without the multiline string literal (""") it works just fine val path = s"""c:\foo\bar\$fileName.csv"""  val path = s"c:\foo\bar\${fileName}.csv" //> path : String = c:\foo\bar\myFile.csv

Further testing to reproduce the issue:

object wcScala10 {

  util.Properties.versionString   //> res0: String = version 2.10.0-RC1
  val name = "James"              //> name  : String = James
  val test1 = s"Hello $name"      //> test1  : String = Hello James
  val test2 = s"""Hello $name"""  //> test2  : String = Hello James
  val test3 = """Hello \$name"""  //> test3  : String = Hello \$name
  val test4 = s"""Hello \$name""" //> java.lang.StringIndexOutOfBoundsException: 
                                  //>   String index out of range: 7
}

Is this exception due to a bug? or am I simply not allowed to use backslash before the $ sign when doing String interpolation?

Here is more of the stacktrace:

java.lang.StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.charAt(String.java:686)
    at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala :39)
    at scala.StringContext$.treatEscapes(StringContext.scala:202)
    at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:90)
    at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:90)
    at scala.StringContext.standardInterpolator(StringContext.scala:120)
    at scala.StringContext.s(StringContext.scala:90)
    at wcScala10$$anonfun$main$1.apply$mcV$sp(wcScala10.scala:9)
    at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
 cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
    at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
 orksheetSupport.scala:65)
    at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
 ksheetSupport.scala:75)
    at wcScala10$.main(wcScal
 Output exceeds cutoff limit.

Update:

Now marked as fixed for Scala 2.10.1-RC1

https://issues.scala-lang.org/browse/SI-6631

By the way, even after the fix, the right way to do interpolation and avoid escaping is using raw:

val path = raw"c:\foo\bar\$fileName.csv"

e.g.

val fileName = "myFileName"               //> fileName  : String = myFileName
val path = raw"c:\foo\bar\$fileName.csv"  //> path  : String = c:\foo\bar\myFileName.csv
like image 292
Eran Medan Avatar asked Nov 08 '12 19:11

Eran Medan


1 Answers

String interpolation notation takes over control of whether a string is a raw string or not. All that triple-quoting gets you is the ability to quote single quotes. If you want no interpolation, use raw"Hi $name" instead. (Except raw is also buggy in 2.10.0; a fix is in for 2.10.1 AFAIK.)

That said, this is not a very friendly way to deal with the situation of having a malformatted string. I'd classify it as a bug, only because it's returning an out-of-bounds exception not something that says an escape code can't be completed.

Note: these break also:

s"Hi \$name"
s"""Hi \"""
like image 50
Rex Kerr Avatar answered Oct 14 '22 11:10

Rex Kerr