tags:

views:

194

answers:

1

I'm using a raw strings but when I print the string I'm getting extra tabs at the start of each line.

val rawString = """here is some text
and now im on the next line
and this is the thrid line, and we're done"""

println(rawString)

this outputs

here is some text
    and now im on the next line
    and this is the thrid line, and we're done

I've tried setting different line endings but that had no effect. I'm working on the Mac (OS X tiger) using jEdit as my editor. I get the same result when I run the script in the scala interpreter or when I write the output to file.

Anybody know whats going on here?

+8  A: 

This problem is due to the fact that you are using multiline raw strings in the interpreter. You can see that the width of the extra spaces is exactly the size of the scala> prompt or the size of the pipe + spaces added by the interpreter when you create a new line in order to keep things lined up.

scala> val rawString = """here is some text          // new line
     | and now im on the next line                   // scala adds spaces 
     | and this is the thrid line, and we're done""" // to line things up
// note that the comments would be included in a raw string...
// they are here just to explain what happens

rawString: java.lang.String =
here is some text
       and now im on the next line
       and this is the thrid line, and we're done

// you can see that the string produced by the interpreter
// is aligned with the previous spacing, but without the pipe

If you write your code in a Scala script and run it as scala filename.scala you do not get the extra tabs.

Alternatively, you can use the following construct in the interpreter:

val rawString = """|here is some text
                   |and now im on the next line
                   |and this is the thrid line, and we're done""".stripMargin

println(rawString)

What stripMargin does is strips anything before and including the | character at the start of every line in the raw string.

EDIT: This is a known Scala bug -- thanks extempore :)

UPDATE: Fixed in trunk. Thanks extempore again :).

Hope it helps :)

-- Flaviu Cipcigan

Flaviu Cipcigan
Thanks Flaviu, very clear explanation
Brian Heylin
It happens with multiline XML too. Now that I think of it... I think this is a bug. There's no reason for the REPL to add stuff I'm not writing myself.
Daniel
Yes, Flaviu has been making so good answers on Scala questions that I answer questions much more rarely nowadays.
Daniel
@Brian: You're welcome :) @Daniel: I thought it was a bug as well, but it seems it still exists in a recent 2.8 nightly build and there is a workaround built in the standard library in the form of `stripMargin` (though `stripMargin` is useful in other circumstances...) Are there any bug reports on this issue that you know of?
Flaviu Cipcigan
@Daniel: Thanks for the thumbs-up as well! The Scala SO community is actually my way of getting as close to real world Scala experience as I can get. I am seriously considering Scala for my computational physics projects, but until I can convince the right persons, most of the Scala I do now is on SO. Though worry not... my SO presence will probably be affected a bit by university starting on Monday :).
Flaviu Cipcigan
I'll certainly be needing a sage to answer more scala questions ;)
Brian Heylin
https://lampsvn.epfl.ch/trac/scala/ticket/2115
extempore
...now fixed in trunk.
extempore