I've written a short function to strip the leading whitespace from a multiline string literal and concatenate the lines, as if you'd written out several concatenated strings or a single very long one.
def stripMultiline(input : String) =
input.split("\n").map(_.dropWhile(_.isWhitespace).stripLineEnd).mkString
It works the way I'd expect in the REPL:
scala> val longString =
| """
| one fish,
| two fish,
| red fish,
| blue fish
| """
scala> stripMultiline(longString)
res0: String = one fish, two fish, red fish, blue fish
However, if I put the same code into a main
method and compile it with scalac
, I see something different:
package substitutions
object Main {
def stripMultiline(input : String) =
input.split("\n").map(_.dropWhile(_.isWhitespace).stripLineEnd).mkString
def main(args : Array[String]): Unit = {
val text =
"""
one fish,
two fish,
red fish,
blue fish
"""
val oneLine = stripMultiline(text)
println(oneLine)
}
}
(back in the console)
C:\KC\code\scala\sub>scala substitutions.Main
blue fish
I'm running Scala 2.10
for both the REPL and Scalac. I've seen the error on Windows 7 32 bit and 64 bit. Can anybody think of why this behavior isn't the same in both versions? It threw me for a loop. Is this a problem in my logic, or should I be filing a bug report?