views:

41

answers:

3

Probably is something really simple that I'm missing, but what's wrong with having a string going in multiple lines?

For instance, Ruby is:

 text = <<END
     Some 
     text 
  END

And Python is:

text = """
   Some 
   text
 """

And C# is:

string text = @"
     Some 
     Text";

Which come closer, but still needs the @ character.

What's wrong with using a single line like this:

 text = "
    Some 
    text
  "

I think in this case, the string literal could end, where the (") appears alone, that way, if quotes are found in the way, they are ignored.

text = "
  He said "This is cool"
  But it wasn't , until "
 " //<-- quote mark alone  

What reason(s) are there to avoid having single quotes multi lines string literal in many programming languages, namely, Java, JavaScript, C#, C++, C, Ruby, Python?

+1  A: 

The only reason I can think of a specific syntax needed is because then you don't have to worry about escaping characters. I'm not sure this is the reason but it's surely an advantage of this syntax.

The last example is a bit problematic when you have to define your syntax because you are using a single token " to do multiple stuff and that's not easy with a standard LALR(1), you'll need more token in input to predict the right rule to use.

More information on LALR parser.

dierre
A: 

I suspect part of this is trying to allow decent formatting.

In C or C++, you can always write a long string as one long string, but this will extend way to the right, and if it wraps around it's going to be ugly. Allowing it to be split up makes it easier to keep the formatting of the program intact. Typically, C and C++ programs do not make such heavy use of multiline strings as to make it useful to change the language to make them more convenient.

It's often useful to explicitly show the end-of-lines and such in a string, and to allow it to keep the formatting of the surrounding program. It's also sometimes useful to be able to write a literal string just the way you want it, and the Ruby, Python, and C# examples show ways to write literal multi-line strings just the way you want them.

Your suggestion intrudes on the formatting, much like the Ruby and Python examples, and really doesn't look like a better solution. It would be too easy to confuse those strings with normal strings, while the Ruby and Perl HERE-documents and Python triple quotation show exactly what those strings are.

David Thornley
+2  A: 

First, it's a horror to parse - depending how simple the rest of the grammar is, this single "convenience feature" may make the frontend orders of magnitude more complex. See dierre's answer for details.

Another reason is that such syntax could be dangerous - forget a closung quote and you've got one huge string and a much smaller program ;)

Third, multi-line strings are not needed that often (especially in languages that concatnate adjacent literal strings even across multiple lines, like C and Python). It just doesn't pay off compared to the above drawbacks.

delnan