[e-lang] Indentation and line breaks in string literals

Lex Spoon lex at lexspoon.org
Mon Mar 3 10:01:16 EST 2008


>

The one thing I'd watch out for in the listings you give is that you  
*do* want to include *some* amount of initial whitespace on  
subsequent lines.  So there must be some way to mark the real  
beginning of the line, versus whitespace that should be ignored

Anyway, to round out your list some, the usual way in Scala is to use  
a multi-line string literal and then use a utility method to strip  
out the unwanted indentation.  The real beginnings of the lines are  
marked by |, and the stripMargin method removes everything up to and  
including that mark on each line.


    """|{
       |  foo
       |  bar
       |}""".stripMargin


The idea is that the | gives a visual cue where the beginning of the  
line is.  There is still noise on the first and last line, though,  
unfortunately.


Also, the C-ish version works fine in Scala and many other languages,  
if you are willing to add a + mark at the end of each line.  I also  
like to add parens around the whole thing, though they shouldn't be  
necessary in this case in Scala source code.  (They are necessary in  
the repl.)

   ("{\n"+
    "  foo\n"+
    "  bar\n"+
    "}")


-Lex


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.eros-os.org/pipermail/e-lang/attachments/20080303/c3bde5ba/attachment.html 


More information about the e-lang mailing list