<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
My thinking pretty much matched Brian’s analysis below, until I saw Archie’s examples and thought about them. Four points:
<div><br>
</div>
<div>(1) I would add one more simplistic argument in favor of “different”: I see some value in a reader of code having fair warning, quite visible and up front, that what looks like a string may actually contain executable code (possibly having side effects).
(Maybe this is related to what Brian meant by "The prefix sigil means no one has to ‘buffer' when interpreting the code.".)</div>
<div><br>
</div>
<div>I think we do want such a warning, if present, to be concise but hard to overlook, and I think the choice of “$” fits that bill admirably. (Pro: The character “$” is associated with string interpolation in a number of other languages, including C#, Dart,
Groovy, JavaScript, Julia, Kotlin, PHP, TCL, TypeScript, and Visual Basic. Con: Of the languages just listed, those that use “$” before the opening double quote are C# and Visual Basic, and the proposed Java syntax is not otherwise identical to the syntax
of C# and Visual Basic, which enclose expressions in _unescaped_ braces.)</div>
<div><br>
</div>
<div>(2) Because “$” is an identifier in Java, it suggests that we can hold open a possible future where we allow other string-prefix sigils having the syntax of an identifier, but without really committing to that generality at this time.</div>
<div>
<div>
<div><br>
</div>
<div>(3) Because “$” is a _discouraged_ identifier in Java (see JLS §3.8: "The dollar sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.”), in practice all occurrences of dollar signs would
in fact flag string templates.</div>
<div><br>
</div>
<div>(4) Archie's suggestion does not create an alternate syntax for the Plain Old String Literals we have had in Java since its inception.</div>
<div><br>
</div>
<div>For these reasons, I recommend that Archie’s suggestion (and perhaps also the C#/Visual Basic variation) be given careful (re-)consideration at this time.</div>
<div><br>
<blockquote type="cite">
<div>On Mar 11, 2024, at 1:36 PM, Brian Goetz <brian.goetz@oracle.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
The overlap between string literals and string template literals is indeed a tricky one, and bears some review of the options. Obviously string templates and strings have some things in common (its in the name!), but they are also different and evaluate to
different types. So how “same” or “different” should they look?
<div><br>
</div>
<div>Simplistic arguments in favor of “different”:</div>
<div> - Ambiguity is bad, clarity is good</div>
<div> - String / string template literals can be both wide and tall; having to examine the entirety to know which it is could be confusing</div>
<div> - Simpler for compiler and specification writers</div>
<div><br>
</div>
<div>Simplistic arguments in favor of “same”:</div>
<div> - Will be perceived as “fussy” or distracting</div>
<div> - Users are already grumpy that we’re not doing “string interpolation” and calling it a day</div>
<div> - Most of the time, it is perfectly obvious which one it is </div>
<div> - Have to make up yet another new and unfamiliar syntax to disambiguate, think of the bike shedding</div>
<div><br>
</div>
<div>There are probably others, but none of these seem like slam-dunks one way or the other. </div>
<div><br>
</div>
<div>There are a few choices here:</div>
<div><br>
</div>
<div> - Keep the current syntax approach</div>
<div> - Give STs a new syntax</div>
<div> - Give both STs and string literals an _optional_ new syntax, such as I_IZ_STRING”…” and TEMPLATZ”…”, but allow the current approach when disambiguation is not needed</div>
<div><br>
</div>
<div>The last seeks a compromise between the current path and the desire for explicitness. Suppose we allowed s”…” and t”…” literals, where the sigils were optional. What then? </div>
<div><br>
</div>
<div>Obviously in the cases which are currently ambiguous-seeming, users could disambiguate explicitly. The prefix sigil means no one has to “buffer” when interpreting the code. That’s nice. Having two ways to write classical string literals might confuse
people who haven’t seen them before, or stimulate unproductive “style wars”. That’s probably not too big a problem here. </div>
<div><br>
</div>
<div>Overall, though, I am not so enthused about creating yet another new lexical mechanism for having different kinds of stringy things. The value is … meh, and it seems an attractive nuisance. In other languages with multiple “flavors” of string, there
is a tendency to proliferate more flavors. (Raw strings, anyone?). </div>
<div><br>
</div>
<div>My take is that this is something that is bothering us a lot because it is new, but I’m skeptical that it carries its weight.</div>
<div><br>
</div>
<div><br>
</div>
<div>
<div><br>
<blockquote type="cite">
<div>On Mar 11, 2024, at 9:07 AM, Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div dir="ltr">
<div dir="ltr">On Mon, Mar 11, 2024 at 9:37 AM Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank">forax@univ-mlv.fr</a>> wrote:</div>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div style="font-family: arial, helvetica, sans-serif; font-size: 12pt;">I vote for making string templates explicit.<br>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>Caveat: I've been following this discussion only loosely so I'm likely to say something stupid/ignorant/redundant; if so please ignore.<br>
</div>
<div><br>
</div>
<div>But I am tending to agree with Remi. The recent simplifications Brian described are a definite improvement, but now we're left with a new question:<br>
</div>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">What is the advantage of having the language literals for String and StringTemplate look so confusingly similar?<br>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">Reversing that question, I'm not seeing the big downside of having a simple prefix for literals like this:<br>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote"> var s = "this is a string";<br>
</div>
<div class="gmail_quote"> var st1 = $"this is a (degenerate) template";</div>
<div class="gmail_quote"> var st2 = $"this is also a \{template}";</div>
<div class="gmail_quote"> var x = "this is a \{lexical_error}";<br>
</div>
myobj.someOverloadedMethod($"this is definitely a template");
<div>
<div> myobj.someOverloadedMethod("this is definitely a string!"); // no need to consult javadoc here<br>
<div><br>
</div>
</div>
</div>
<div>Seems like the trade-off is straightforward:</div>
<div><br>
</div>
<div class="gmail_quote">Cost: one character<br>
</div>
<div class="gmail_quote">Benefit: instant disambiguation clarity in the developer's mind<br>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">At least, it makes the whole API design/overload question straightforward.<br>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">Put another way, StringTemplates are a cool new language feature, and as such it seems like they deserve a "first-class" allotment in the syntax of the language.<br>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">-Archie</div>
<div class="gmail_quote"><br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</body>
</html>