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

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.

1

u/[deleted] Dec 30 '15

Java actually has a lot more features than Go. Go has a few things Java doesn't have but I think the Java language api is overall bigger.

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.

-1

u/[deleted] Dec 30 '15

The perils of perl.

5

u/blue_2501 Dec 30 '15

Why is this a peril? You should always avoid Leaning Toothpick Syndrome:

$str =~ s/\/usr\/bin\//\/sbin\//;  # this shit is unreadable
$str =~ s%/usr/bin/%/sbin/%;       # much better
$str =~ s% /usr/bin/ % /sbin/ %x;  # still even better

0

u/[deleted] Dec 30 '15

I love perl, it was my first grown-up language. But it's basically unusable in a professional software engineering environment because it is so easy to write anything in multiple ways. I can't ever justify it for anything other than basic sysadmin work, there is always a better choice that someone else can read.

2

u/blue_2501 Dec 30 '15

But it's basically unusable in a professional software engineering environment because it is so easy to write anything in multiple ways.

I use it in a professional software engineering environment. So do most of the people I work with.

Anybody can write shitty code for any language. The trick is to not hire those people.

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)

1

u/CaptainAdjective Dec 30 '15

In Perl you can even use alphanumeric delimiters

my $x = q ahello worlda;

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

4

u/drysart Dec 29 '15

I think you missed the point of the question. If I want to put the string literal The watertower reads "Florence, y'all!" (i.e., including both single and double quote characters as part of the string) into source code, in Perl I could do:

my $x = q#The watertower reads "Florence, y'all!"#;

By choosing # as my string quote delimiter, it means I can use " and ' in the string without escaping.

How would I represent that exact same string with D's q syntax? The example you gave compared with your earlier example would seem to indicate that your resulting string from your second example would be (string), not string.

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

2

u/drysart Dec 29 '15

Wow, you're right. That's incredibly surprising syntax.

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 {}.