JDK-8061408 : Add support for verbatim string literals
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Priority: P4
  • Status: Closed
  • Resolution: Other
  • Submitted: 2001-06-21
  • Updated: 2014-10-17
  • Resolved: 2014-10-17
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description
Name: bsC130419			Date: 06/20/2001


java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

The current syntax for string literals, as stated in the JLS, has many
practical limitations:

A) String literals that contain many occurances of the '\' character must use
superfluous escaping. Examples of this limitation are:
1) Regular expressions
2) Path syntaxes that use '\' as a path separator, eg. Win32 file paths.

B) String literals cannot contain line breaks. Often the author of the Java
source file would like to add structure, by using whitespace, to the String
literal without breaking the meaning. For instance:

1) Storing a SQL query as a String literal. Using the current version of the
Java Language, the source file author can either put the whole SQL statement in
one line, or break the statement in separate chunks:

String qry =
"SELECT a.name, b.number" +
"FROM table a, table b" +
"WHERE a.id = b.id" +
"ORDER BY name";

The above example is small, but bigger queries quickly start to require a lot
of tab,",+ typing.


Feature Request Proposal

A vertabim string literal would have the following properties:
1) All characters are treaded vertabim, that is, they are not escaped.
1a) Character, hexadecimal and Unicode escape sequences are not processed.
1b) The only character with a special meaning is the tail quote, indicating the
end of the vertabim string literal.
2) A vertabim string may span multiple lines, only ending when the end quote
delimiter is encountered.
3) A vertabim string literal is indicated by a '@' character immediately before
the opening quote: String re = @"\(\d{1,3}\D{1,3)";

  To summarize, I propose adding vertabim String literals as formulated in the C#
Language Specification[1] submitted to the ECMA, paragraph 9.3.4.5.
A similar enhancement is proposed for the next version of ECMAScript (v4).

A number of examples follow to summarize the proposed syntax, and show the
advantage of vertabim string literals.


Examples
(string literal, result string in comment)

String a = "hello, world";                      // hello, world
String b = @"hello, world";                     // hello, world

String c = "hello \t world";                    // hello 	 world
String d = @"hello \t world";                   // hello \t world

String e = "Joe said \"Hello\" to me";		// Joe said "Hello"
String f = @"Joe said ""Hello"" to me";         // Joe said "Hello"

String g = "\\\\server\\share\\file.txt";	// \\server\share\file.txt
String h = @"\\server\share\file.txt";		// \\server\share\file.txt

// A simple query, much more easily separated over multiple lines.
// Also note that indenting is made much easier for these type of text.

String qry = @"
   SELECT
     a.name, b.number
   FROM
     User a, Data b
   WHERE
     a.name = "James"
      AND
     a.id = b.id
";

// A simple regular expression, without the need for erroneous escaping:

String RE = new RE(@"\D{1,3}\S{1,3};");
(Review ID: 126782) 
======================================================================

Comments
The 'specification' component of the Java Bug System is for reporting technical errors and ambiguities in the text of The Java Language Specification and The JVM Specification. It is not the venue to propose new features in the Java language or JVM. Ongoing feature development is carried out in OpenJDK (http://openjdk.java.net/jeps/); corresponding enhancements to The Java Language Specification and The JVM Specification are managed through the Java Community Process (http://jcp.org/).
17-10-2014

WORK AROUND Name: bsC130419 Date: 06/20/2001 None ======================================================================
31-07-2004

EVALUATION This is yet another request for a syntactic sugar to save some user from typing. It's not worth it. gilad.bracha@eng 2001-06-28
28-06-2001