Looking at the API spec of STR [1], which will be extremely widely read, I agree that more normative text is needed about the form of the result string. A single example-led mention of an "interleaving concatenation" is insufficient, and the example's use of arithmetic + in the string template is confusing given that everyone will be thinking about Java's concatenating +. (The 10+20 example is already shown in the interface-level javadoc anyway.)

I also have some sympathy with the complaint that a reference to the field STR in a template expression is not being compiled in line with JLS 13.1 (item 4), which demands a symbolic reference to StringTemplate and thus predicts that the interface will be loaded. 13.1 doesn't specify how every construct is compiled, so it's legitimate for a compiler to implement a template expression without the JLS having to define "string template runtime" ... but it would be best for 13.1 to carve out some fields (STR, RAW) from the usual rules.

Alex

[1] https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/StringTemplate.html#STR -- I note that the field STR is basically unfindable with javadoc search, despite being omnipresent (auto-imported) in new Java programs.

On 7/25/2023 2:38 AM, Remi Forax wrote:
Currently javac uses a trick to rewrite STR."" template to more or less the 
string concatenation equivalent, by passing the call to the bootstrap method of the 
string template runtime.

But this rewriting
- has a side effect at runtime, the class StringTemplate is not loaded.
- exact behavior is slightly different from the string concatenation, constant 
strings are not pre-computed, so the exact behavior is not an existing behavior.
- only works with the unqualified STR (where STR is a static import implicit or 
explicit) but not with a qualified reference to StringTemplate.STR.

I think we need something in the spec saying that this is a valid source code 
transformation specific to STR, that it works in the qualified and unqualified 
case and that the transformations should be the same as with the string 
concatenation.

regards,
Rémi


Reply via email to