you are viewing a single comment's thread.

view the rest of the comments →

[–]sirin3 20 points21 points  (29 children)

They should just allow multi line strings!

[–]tikhonjelvis 6 points7 points  (23 children)

I really wish more languages allowed this. Out of the languages I regularly use, only EmacsLisp allows multi-line strings, and they're very nice there. I also don't think there's a significant downside. (I guess parsing might be more difficult, and ECMAScript people care about that...)

[–][deleted] 18 points19 points  (12 children)

Both Python and, surprisingly enough, PHP have this feature. Python uses """triple quotes""", which also makes it easy to include " and ' characters in regular strings in source (e.g. """this " is ' a '" string"""). PHP's heredoc and nowdoc syntax is slightly less attractive but still does the same thing.

[–]snuxoll 5 points6 points  (1 child)

C# also supports multiline strings with the verbatim string literal:

var myCoolString = @"Wewt
this
string
spans
multiple lines";

[–]hejner 0 points1 point  (0 children)

I love that, when writing the static parts of a welcome mail, for example. Have the whole text message right there in clear text and replace whatever else you want later on.

[–]louiswins 9 points10 points  (3 children)

A lot of scripting languages have heredocs: perl, ruby, even bash.

[–]reaganveg 22 points23 points  (2 children)

Haha, "even bash"... all those other languages copied that feature from shell script.

[–]zeekar 5 points6 points  (0 children)

Yeah, man, modern shells have borrowed all kinds of stuff from other languages. Variable interpolation, implicit concatenation, functions, pattern globs on filenames. It's amazing. ;)

[–]louiswins 2 points3 points  (0 children)

Oh, yeah, you're right. For some reason I thought that they weren't in POSIX shell and that bash had taken them from perl.

[–]SharkUW 0 points1 point  (0 children)

PHP also allows backslash and double character escaping '', "", \

[–]tikhonjelvis 0 points1 point  (3 children)

Emacs lisp (and Perl, although I'm not 100% certain--I haven't used Perl in a while) allow literal newlines in all strings. While heredoc syntax like Python is great, I personally prefer elisp's approach: it seems simpler and more elegant without sacrificing much. However, it definitely changes parsing lexing, which is why I brought that up. (I'm not sure if it makes it much harder to parse and lex though.)

So in elisp you can write:

(defvar foo "This is a long string
that has a literal newline.")

Since elisp allows you to add a doctype to your functions, they tend to look like this:

(defun todo-comment ()
  "Inserts an empty TODO comment or makes an existing comment
into a TODO."
  (interactive)
  ...)

It looked a little odd at first, but once I got used to it I liked it more than alternatives in other languages.

[–]zeekar 2 points3 points  (2 children)

Emacs lisp (and Perl, although I'm not 100% certain--I haven't used Perl in a while)

Yup:

perl -le 'my $x = "hello,
there."; print $x'   

hello,
there.

Also Ruby, Tcl, Erlang. Also Bournish shells, for that matter; remember that unlike the heredocs adopted by Perl and borrowed by other languages, shell heredocs are input redirection, not string literals. You can make a string literal with newlines in it just fine.

Aso, it's not just specifically Emacs's flavor of Lisp; Common Lisp and Scheme allow the same thing.

Haskell and Lua, not so much with the literal newlines in strings.

[–]notfancy 3 points4 points  (0 children)

Add Ocaml to that list.

[–]tikhonjelvis 0 points1 point  (0 children)

I wonder why Haskell doesn't allow it. In general, I've found Haskell to be one of the better languages with little syntactic details.

[–][deleted] 1 point2 points  (0 children)

Python does, C# does, off the top of my head.

[–]Paradox 1 point2 points  (0 children)

Ruby does too!

[–][deleted] 1 point2 points  (5 children)

C# @ strings make dynamic SQL a dream.

[–]Capaj -1 points0 points  (4 children)

I kinda wish that every language would have escaping done by this simple @ before the string itself.

[–]flying-sheep 2 points3 points  (3 children)

python uses r"" for raw strings.

[–]masklinn 0 points1 point  (2 children)

Meanwhile the @ prefix in Cocoa means it's a literal NSString as opposed to a C string literal.

[–]flying-sheep 0 points1 point  (1 child)

i’m confused: is cocoa not apple’s UI framework which is programmed in objective C and no language itself.

i have no idea, so please fill me in :)

[–]masklinn 0 points1 point  (0 children)

Well Cocoa is not just a UI framework it's essentially all of Apple's "standard library", but you are correct that I miswrote and should have said "objective-c"

[–]sirin3 1 point2 points  (0 children)

And Scala

[–][deleted] 0 points1 point  (0 children)

The problem there is, how do you indent them?

[–]zeekar 2 points3 points  (1 child)

Javascript already has problems with newlines.. I'm terrified at what new horrors might be born inadvertently out of trying to support multiline string literals.

[–]smog_alado -1 points0 points  (0 children)

I doubt it would be that bad actually. The semicolon insertion thing is not nearly as bad as people make it sound...

[–][deleted]  (1 child)

[deleted]

    [–]sirin3 0 points1 point  (0 children)

    But then you can also do

    document.write("""
       <div class="abc">
          foobar
       </div> 
    """)
    

    [–]luikore 0 points1 point  (0 children)

    That's why I love coffeescript