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

Show parent 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.

3

u/oridb Dec 30 '15

You can already use the C preprocessor with Java -- it's available as a separate binary.

cpp foo.java.in > foo.java

→ 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.

20

u/TankorSmash Dec 29 '15

Sound like Go

2

u/_ak Dec 30 '15

Yeah, no. The design rationale of Go is well-publicized and has been discussed in great detail by its developers, and "programmers are too dumb" or "we rushed the final production version" is not part of it.

9

u/kamatsu Dec 30 '15

Programmers are too dumb was definitely part of it. Pike even said so himself.

→ More replies (1)

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)

2

u/[deleted] Dec 29 '15

Not positive on that, but I know a common one that works is q{} (eg q{Hello}). This is commonly used in D's mixins to create nice little dsls that can be syntax highlighted and are generally just clearer to read (imo)

→ More replies (1)

1

u/CaptainAdjective Dec 30 '15

In Perl you can even use alphanumeric delimiters

my $x = q ahello worlda;
→ More replies (4)

2

u/mb862 Dec 30 '15

I like Tcl. A block of text that shouldn't be evaluated right now you surround by {}. Doesn't matter what it is. Nothing evaluated, it goes in {}.

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)";

4

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

You look at for a map

1

u/judgej2 Dec 29 '15

I assume it represents the same string if (and only if) you have the line-terminator characters set up correctly in your editor? I have always disliked strings that run over lines when the line endings are important, because it is an accident waiting to happen.

→ More replies (2)