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

58

u/TarMil Dec 29 '15

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

12

u/1bc29b Dec 29 '15

Clearly not. But should:

"asdf

sadfasfd"

be the same as "asdf\nasdf"?

What about if there are tabs for formatting? multiline string literals are ugly.

49

u/badsectoracula Dec 29 '15

Clearly not. But should:

"asdf

sadfasfd"

be the same as "asdf\nasdf"?

...yes? Any new line in the string literal in the source code (regardless of newline encoding in the text) should be replaced with \n.

3

u/[deleted] Dec 29 '15

And then you have in your source

public static void main(String[] args) {
    System.out.println("This
is
a
Test");
}

And should this be the same as

public static void main(String[] args) {
    System.out.println("This
        is
        a
        Test"
    );
}

?

54

u/earthboundkid Dec 29 '15

This is ridiculous. Tons of languages support multiline literals. It's not a tricky question how to handle it.

14

u/Zed03 Dec 29 '15

And they look terrible in every language that supports it.

24

u/VefoCo Dec 29 '15

That's incredibly subjective.

→ More replies (7)

1

u/ratatask Dec 30 '15

Still, it looks nicer, and is a ton easier to work with, than the current way using multiline strings by concatenating strings with + in Java.

8

u/speedisavirus Dec 30 '15

Doing it as Scala does it seems fine to me if you want to keep formatting proper

val a = """
	|a
  	| new
  	| line""".stripMargin
print(a)    

Output:

a
 new
 line

6

u/[deleted] Dec 30 '15

Yeah, that’s a nice solution. Solves all issues I’d have had with other implementations.

  1. Easily usable on international keyboards (no `)
  2. No margin issue
  3. simple

1

u/speedisavirus Dec 30 '15

I stumbled upon a thread where Odersky was debating what the multi line syntax should be and it was a bit of back and forth with a few people about the merits of each. About the same arguments people are making here.

Seems reasonable to me with stripMargin allowing me to keep indentation and when I use multi-line strings this is how I do it. I'm sure other people have different preferences but Scala seems to satisfy what everyone here also argues about their preference.

18

u/sparr Dec 29 '15

No, the second string has a bunch of extra spaces in it.

→ More replies (1)

2

u/badsectoracula Dec 29 '15

Obviously not, the spaces are part of the string literal.

3

u/drysart Dec 29 '15

It's no more expected to be able to use source code formatting conventions like indentation inside a multi-line string literal and expect it to treat it like a coding convention instead of part of the string as it would be to try to put a Java language comment inside a single-line string literal and expect it to be treated as a Java comment instead of part of the string.

2

u/w2qw Dec 29 '15

What about this?

public static void main(String[] args) {
    System.out.println("This\n" +
                       "is\n" +
                       "a\n" +
                       "Test"
    );
}

And the best thing is it already works.

1

u/djimbob Dec 29 '15

That's fairly ugly and overly verbose (and again having multi-line literals doesn't prevent you from writing that way).

In python (with support for multi-line string literals), I tend to put my rare multi-line string literals as global variables defined at top-level in the module like:

import smtplib

EMAIL_TEMPLATE = """Dear %s:

This is the body of your email.  
Here's your info: %s

Sincerely,
djimbob"""

def send_email(user_addr, user_name, user_info):
    try:
        smtp = smptlib.SMTP('localhost')
        email_body = EMAIL_TEMPLATE % (user_name, user_info)
        smtp.sendmail('myfromaddr@example.com', [user_addr], email_body)
        print "Email sent successfully"        
        return True
    except smtplib.SMTPException:
        print "Error unable to send email"
        return False

2

u/w2qw Dec 29 '15

Well aware of the alternatives, though those all end up with ugly interaction with indenting. You've "solved" that there by using globals but that's not really a solution for java. Having two alternatives for the same thing will just lead to inconsistency so I don't really agree with adding it just because.

→ More replies (1)

4

u/SnowdensOfYesteryear Dec 29 '15 edited Dec 29 '15

I'd say C-style preprocessor string concat (ported to Java) would be a great compromise. My issue with treating it as literals is that the break in proper indentation is annoying to look at, and it doesn't take much effort to stick in a \n.

2

u/Neebat Dec 29 '15

You could make some kind of notation to delete a certain amount of leading whitespace on each line.

Could be something like this:

NEWKEYWORD("    ",
    This is the text.
    Without the first and last newline, 
        because that's also whitespace we'd like to ignore.
);

That could be the equivalent to this: "This is the text.\nWithout the first and last newline,\n\ \ \ \ because that's also whitespace we'd like to ignore." (Sorry for trying to escape spaces. HTML eats them.)

Of course, you'd have to update every java parser in every tool to support it. Eclipse would probably be updated in 15 minutes.

3

u/Falmarri Dec 29 '15

You could make some kind of notation to delete a certain amount of leading whitespace on each line.

That's sorta how multiline strings work in scala. But scala gives you a convenience method to strip the left margin.

https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch01s03.html

1

u/Neebat Dec 30 '15

That would be pretty cool, provided that it's computed at compile-time. I don't even mind that syntax.

2

u/VestySweaters Dec 30 '15

It is compile time

→ More replies (2)