Strange error with line interpolation

I get a weird error with the following code.

I have an Example class with a companion object in which I defined a string, SIGN . In the Example class, I have a method where I create a regular expression and use string interpolation so that I can use SIGN to create my regular expression.

This compiles, but at runtime I get a strange error. Is this a Scala bug? I am using Scala 2.10.3 (on Windows 7).

 scala> :paste // Entering paste mode (ctrl-D to finish) class Example { import Example._ def regex = s"""$SIGN?\d+""".r } object Example { private val SIGN = """(\+|-)""" } // Exiting paste mode, now interpreting. defined class Example defined module Example scala> val e = new Example e: Example = Example@77c957d9 scala> e.regex scala.StringContext$InvalidEscapeException: invalid escape character at index 1 in "?\d+" at scala.StringContext$.treatEscapes(StringContext.scala:229) 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:123) at scala.StringContext.s(StringContext.scala:90) at Example.regex(<console>:9) 
+6
source share
1 answer

Having looked closely at the stack trace, I see what is happening.

You can see that the s method executes on a line that handles the replacement of $SIGN . This method encounters \d in a string and apparently tries to translate this; see call treatEscapes in the stack trace. The treatEscapes method treatEscapes not recognize \d and throws an exception.

It can be fixed by writing \\d in the string, but this defeats the whole goal of having a string with three quotes ...

Conclusion: String interpolation and triple quotes seem to be related to each other. I would say that this is a Scala bug. (Why does method s apply to screens?).

edit - It really looks like a SI-6476 error, as Travis Brown pointed out in a comment.

+6
source

All Articles