This comment is not about text blocks as such but the write up by the author. (Brian Goetz... If you are here, when do we see the next edition of the book, please?).<p>It is not simply 'here's a new language feature'. I find it quite informative regarding what, why, how and why not etc. His articles explain what went into the decision making. Something on the lines of discussions in Effective Java by Joshua Bloch.<p>His article on records: <a href="https://www.infoq.com/articles/java-14-feature-spotlight/" rel="nofollow">https://www.infoq.com/articles/java-14-feature-spotlight/</a>
This is a nice feature to have, no doubt, but won't make much difference for an IntelliJ user. What I am most eagerly waiting for is Loom, that will bring fibers to the JVM, and make the arguments in favour of server side JVM even more stronger.
So this is a long overdue QoL feature but the whole whitespace elision thing is unwelcome and unnecessary (IMHO). What's wrong with What You See Is What You Get? This is a perfect example of added complexity to solve a complete non-problem just based on probably the unsubstantiated aesthetic view of a handful of key people. Stop. Just... stop.<p>Even stripping trailing whitespace is... risky. Why is the language trying to decide what whitespace is significant? Or even trying to divine the mind of the developer at all?<p>I didn't realize there was a raw string proposal in Java 12 using backticks (bleh). By now I'm pretty used to languages that use double-quotes for interpreted strings (including escape sequences, at a minimum) and single-quotes for raw strings. This is an incredibly useful feature. Of course, Java uses single-quotes for character literals so that's out, otherwise I'd like to see """ for interpreted and ''' for raw.<p>Anyway, let me point out just one reason why this is terrible: source control. Consider:<p><pre><code> String s1 = """
<outer>
<inner>foo</inner>
</outer>
"""";
</code></pre>
Now change this to:<p><pre><code> String s1 = """
<outer>
<inner>foo</inner>
</outer>
"""";
</code></pre>
It's only one line change but you have in fact changed what the previous lines do.<p>And of course (which the article mentions).... tabs!<p>I really want the option of pasting text into a block like this and knowing that's what I'll get. I don't want to process it usually (eg escaping characters).
This seems a little too magical for Java - normally indentation doesn’t have programmatic meaning like this in Java. I prefer the Scala approach: <a href="https://alvinalexander.com/scala/how-to-create-multiline-strings-heredoc-in-scala-cookbook/" rel="nofollow">https://alvinalexander.com/scala/how-to-create-multiline-str...</a><p>Just seems more obvious/clear.
> To disentangle the incidental and essential indentation, we can imagine drawing the smallest possible rectangle around the XML snippet that contains the entire snippet, and treating the contents of this rectangle as a two-dimensional block of text. This "magic rectangle" is the contents of the text block, and reflects the relative indentation between the lines of the block but ignores any indentation that is an artifact of how the program is indented.<p>Well, I'll withhold judgement somewhat until I eventually use this feature for a little while, but based on the description this is simply too complex, which is disappointing. Who exactly was asking for the "magical rectangle" approach, anyway? Or incidental vs essential indentation? They did too much. And given how long people have been waiting for simple multi-line strings, and how many other languages already have them, I'm not sure how this got messed up.
> Avoid mixing spaces and tabs in the indentation of a text block<p>This has some implications. For example, I can imagine someone editing a text block and inadvertently using a different indentation type (tabs/spaces) from the other existing lines, which would then change their program in a non-obvious way. I'm myself an author of a programming language with a non-standard approach to whitespace (called Muon), so this is a topic I've thought about quite a bit. Interesting that the Java team considers this an acceptable tradeoff. I'm looking forward to seeing how this plays out in practice as a data point.
I much prefer Cs solution to this problem; you can write multiple literals in sequence and they will be concatenated:<p><pre><code> char* text = "hello" " dear"
" sir";
</code></pre>
This is great, because it doesn't make leading whitespace significant on the lines which the text block spans.<p>The Java approach, which seems similar to the Python approach, looks really silly when you are defining a literal in a nested scope;<p><pre><code> def hello():
def world():
text = """hello
world"""
print text</code></pre>
Looooong after the other languages have done it, Java comes around to doing it. Seriously, the best thing about Java is the JVM where you can choose a better language than Java to program with it and get all the benefits of the JVM.
It seems strange to embed content in code in this way. You lose syntax formatting if it's structured content (HTML, JSON, etc) and also require recompiling if the content changes. I understand doing it if you're prototyping/hacking things together but it seems like a lot of effort was put into building this feature.
I don't hate Java. It was my 2nd or 3rd language, and the one I first used professionally. But O-M-G, this feature exists in most mainstream language and has for the past decade <i>at least</i>.<p>This is nuts to me how behind Java is. Is it only due to the backward compatibility? Or is there some crazy red tapping every time something is added?