Enhancing Java String Literals Round 2

Richard Clark rgc197.at.zepler.org at gmail.com
Sat Jan 5 05:58:05 UTC 2019

Have end-of-line strings been considered previously?

With end-of-line strings a marker indicates that the remaining characters
on a the line form a string, a bit like end-of-line comments. Adjacent
end-of-line strings are merged together into one multi-line block. They
support raw strings with only the minor inconveniences of having to start
each line with a marker and having put subsequent parts of the statement on
a new line. Most good text editors support a column editing mode which make
the marker per line a non-issue.

  EndOfLineString {EndOfLineString}

  EndOfLineStringMarker {InputCharacter} LineTerminator

Currently '' is an illegal sequence in Java (I think), so it might be a
good candidate for EndOfLineStringMarker.

Below are some of the examples from Jim Laskey's summary in end-of-line
string form:

String json =
  ''  "name": "Jean Smith",
  ''  "age": 32,
  ''  "location": "San Jose"

    ''"C:\Program Files\foo" bar


String html =
    ''  <body>
    ''    <p>Hello, World!</p>
    ''  </body>

String nested =
    ''String EXAMPLE_TEST = "This is my small example "
    ''    + "string which I'm going to "
    ''    + "use for pattern matching.";
    ''System.out.println(EXAMPLE_TEST.replaceAll("\\s+", "\t"));

Richard Clark

On Fri, 4 Jan 2019 at 17:46, Cay Horstmann <cay.horstmann at sjsu.edu> wrote:

> As raw strings are revisited, I hope that their design leaves room for
> other string handling, in particular interpolation, but perhaps also
> formatting, foreign syntax checking, etc.
> Looking at Jim's submission [1], I was encouraged by seeing the R"..."
> syntax with a prefix that leaves an opening for evolution.
> Using currently unused escapes is another good idea. For example, one
> could envision using \$ for interpolation. Again, room for evolution.
> Is one mechanism for evolution better than the other? The prefix
> mechanism can allow programmers and library authors to provide all sorts
> of implementations, as we know from Scala, JavaScript, etc. That's good,
> but we also know that they don't compose well. For example, what if you
> want interpolation in an otherwise raw string?
> Adding escapes probably presumes that any new string features are part
> of the language, not a library. That's been more in line with the
> previous attempt to define raw strings. I think it is a good idea to
> give this some thought, even though it looks unfamiliar at first.
> As a minor technical matter, using \+ and \- as rawness delimiters
> leaves open the question about raw strings containing \-. Some other
> mechanism could be used as an alternative, e.g starting with \======
> (with one more = than the longest such sequence in the raw run) and
> ending with the same number of =. Or any of a number of other
> well-understood schemes.
> var regex = "\+"\.{3-}"\-";
> var myNameInABox = "\======
> +=====+
> | Cay |
> +=====+
> \======";
> var aNameInQuotes = "\"\${name}\"";
>     // String interpolation (maybe in the future)
> Cheers,
> Cay
> [1] http://cr.openjdk.java.net/~jlaskey/Strings/RTL2/index.html
> --
> Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com

More information about the amber-spec-observers mailing list