Enhancing Java String Literals Round 2

Cay Horstmann cay.horstmann at sjsu.edu
Fri Jan 4 17:45:02 UTC 2019


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