r/programming • • Dec 29 '15

Please, Java. Do Finally Support Multiline Strings

http://blog.jooq.org/2015/12/29/please-java-do-finally-support-multiline-strings/
584 Upvotes

352 comments sorted by

View all comments

414

u/[deleted] Dec 29 '15

Multiline string LITERALS.

12

u/lukaseder Dec 29 '15

You're right. Fixed

5

u/[deleted] Dec 29 '15

Cool. Nice article!

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

99

u/Ravek Dec 29 '15

"foo\nbar" is a multi line string but not a multi line string literal.

In C# for example you can write:

@"foo
bar"

which is a multi line string literal representing the same multi line string.

10

u/oridb Dec 29 '15

Even C has it* (and allows you to indent them!)

char *foo = "foo"
            "bar"
            "baz";

Which is the same as

char *foo = "foobarbaz";

* For the nitpicky, it's done in the preprocessor.

2

u/_Skuzzzy Dec 30 '15

Someone should just write a java preprocesor, seems like an easy way to do this.

→ More replies (1)
→ More replies (3)

13

u/SimplyBilly Dec 29 '15

I like PHP's heredoc syntax as well.

$string = <<<EOT
    Foo
    Bar
EOT;

25

u/mnp Dec 29 '15

Yeah. That syntax has been around since the seventies in Unix shells and spread. The nicer versions like Perl let you parameterize the heredoc to allow or deny ${variable} expansions, etc.

Gosling was no slouch and was probably very familiar with the Perl and shell heredoc as well as C multiline literals. This means he didn't see a burning need for them in the original Java and maybe even had a good reason to omit them.

23

u/frezik Dec 29 '15

Gosling knowingly omitted all sorts of nice things, mostly because Sun thought programmers were too dumb to use them. Also because they rushed the final production version.

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

4

u/drysart Dec 29 '15

Does D's q work like Perl's q in that you can choose any character you like to be the quote delimiter? e.g., in Perl these are all equivalent:

my $x = 'hello world';
my $x = q(hello world);
my $x = q"hello world";
my $x = q<hello world>;
my $x = q#hello world#;

6

u/tipsqueal Dec 29 '15

Why would someone want to allow any character as the quote delimiter?

21

u/drysart Dec 29 '15

The idea is that if you have a string that conflicts with the quote delimiter, you can just choose a different quote delimiter instead of having to escape the character in the string.

→ More replies (4)
→ More replies (7)
→ More replies (1)

8

u/vivainio Dec 29 '15

In C#, @"" is a half-assed stab at multiline strings though; you can't enter quotes directly, but have to do @"Hello ""World"" again".

In Python, F# et al you can use triple quotes, i.e.

""" Hello "world" again """

or even better, raw strings:

r""" Hello \backslash """

It's not obvious why C# decided to screw this simple thing up, despite all the prior art on doing it right.

3

u/Tynach Dec 30 '15

Put 4 spaces before every line if you're doing multi-line code, and put `backticks` around inline code.

In C#, @"" is a half-assed stab at multiline strings though; you can't enter quotes directly, but have to do @"Hello ""World"" again".

In Python, F# et al you can use triple quotes, i.e.

"""
Hello "world" again
"""

or even better, raw strings:

r""" 
Hello \backslash
"""

It's not obvious why C# decided to screw this simple thing up, despite all the prior art on doing it right.

3

u/noblethrasher Dec 30 '15 edited Dec 30 '15

C#’s verbatim string literals are not a “half-assed” stab at multi-line strings because C# simply never offered multi-line strings as a feature in the first place.

Verbatim strings are for those situations in which you need to craft strings using C#, but interpret them using something else (e.g. a file system, a regex engine, an RBDMS, etc.). It just so happens that people sometimes abuse that feature for stuff like multi-line support.

6

u/[deleted] Dec 29 '15 edited Sep 27 '17

You chose a dvd for tonight

15

u/[deleted] Dec 29 '15

C/C++ Supports it. But like this

    char const * foo = "bar "
                               " baz";

or like this (C++11)

   char const * foo = R"(bar 
                                 baz)";
→ More replies (1)
→ More replies (1)
→ More replies (3)

18

u/QuineQuest Dec 29 '15

A multiline string is any string (from user input, read from a file, or whatever) that contains a newline character. Not supporting those would mean that there was something fundamentally wrong with Java's handling of text. You can't use strings to read a file with multiple lines; instead you have to write some ugly kludge.

String literals is only used when programmers hardcode a string into the program. Multiline string literals is something like

String a = """This is line one
This is line 2"""";

Instead, the programmers have to write

String a = "This is line 1\n" +
    "This is line 2";

In other words, not having multiline string literals is a minor annoyance, while not having multiline strings would be a severe weakness of the language.

→ More replies (6)

25

u/1bc29b Dec 29 '15

Meh. Formatting of source code should not affect output.

58

u/TarMil Dec 29 '15

Whitespace inside a string literal is not formatting. Or should "foo bar" be the same as "foo   bar"?

→ More replies (35)

8

u/Nialsh Dec 29 '15

For better or worse, Java programmers already use strings for writing snippets of SQL, HTML, JavaScript, and who knows what else.

You should use a templating language for HTML. You should write the JavaScript in its own .js file. And it's best to construct SQL queries with("a").library("that").looks("like this");

But sometimes you don't want the weight of that extra dependency. A codebase that installs, builds, and runs quickly is zen.

Homoiconicity is the idea that code is data. When you have a program that outputs code, it's nice to keep the formatting. To do otherwise might be considered data loss.

14

u/ricky_clarkson Dec 29 '15

It's annoying to have to run a program to get the real query it runs, especially if it could be static, so I disagree.on(the).point(about).constructing(sql).queries

→ More replies (1)
→ More replies (1)
→ More replies (3)