CSR :
|
|
Relates :
|
|
Relates :
|
Summary ------- Enhance the Java language by introducing _text blocks_, a more intuitive way to represent multi-line strings than string literals. Problem ------- In Java, embedding a snippet of HTML, XML, SQL, or JSON in a _string literal_ ([JLS 3.10.5](https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.10.5)) "..." usually requires significant editing with _escape sequences_ ([JLS 3.10.6](https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.10.6)) and _concatenation_ ([JLS 15.18.1](https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.18.1)) before the code containing the snippet will compile. The snippet is often difficult to read and arduous to maintain. More generally, the need to denote short, medium, and long blocks of text in a Java program is near universal, whether the text is code from other programming languages, structured text representing golden files, or messages in natural languages. On the one hand, the Java language recognizes this need by allowing strings of unbounded size and content, but on the other hand, it embodies a design default that strings should be small enough to denote on a single line of a source file, and simple enough to escape easily. Solution ------- The Java language can be regularized by accepting that strings may be large enough to span multiple lines of a source file, and by envisaging that escapes in their content may represent formatting and layout operations as well as individual characters. Accordingly, it would improve both the readability and the writeability of a broad class of Java programs to have a linguistic mechanism for denoting strings more literally than a string literal -- across multiple lines and without the visual clutter of escapes. In essence, a two-dimensional block of text, rather than a one-dimensional sequence of characters. The following are examples of text blocks: ``` String simple = """ A simple multi-line text block """; ``` ``` String empty = """ """; ``` ``` String html = """ <html> <body> <p>Hello, world</p> </body> </html> """; ``` ``` String query = """ SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB` WHERE `CITY` = 'INDIANAPOLIS' ORDER BY `EMP_ID`, `LAST_NAME`; """; ``` ``` ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); Object obj = engine.eval(""" function hello() { print('"Hello, world"'); } hello(); """); ``` Specification ------- Proposed changes to the Java Language Specification are attached, and also [available online](http://cr.openjdk.java.net/~abuckley/jep355/text-blocks-jls-20190603.html). Because a text block is a constant expression of type `String`, it is acceptable to use a text block anywhere that a string literal could be used, and vice versa. There are no changes to the JVM Specification. A string in the constant pool of a `class` file ([JVMS 4.4.3](https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-4.html#jvms-4.4.3)) has always been independent of Java language rules for string literals, so it is a suitable compilation target for text blocks. A `class` file does not record whether a string in the constant pool was compiled from a string literal or a text block.
|