<div dir="ltr"><div dir="ltr"><div dir="ltr">On Thu, Jul 24, 2025 at 9:16 PM Rob Ross <<a href="mailto:rob.ross@gmail.com" target="_blank">rob.ross@gmail.com</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 dir="ltr"><div>First, I welcome suggestions and feedback on what kind of information is useful to contribute to the topic of string templates in Java.</div></div></blockquote><div><br></div>There was <a href="https://openjdk.org/jeps/465" target="_blank">some preliminary work</a> on this but it got shelved for various reasons.</div><div class="gmail_quote"><br></div><div class="gmail_quote">I hope Brian will let me know if the following comments are out-of-scope... :)</div><div class="gmail_quote"><br></div><div class="gmail_quote">My own current thinking on this topic is that instead of trying to define the one true template syntax, Java should take a "pluggable syntax" approach to this problem.</div><div class="gmail_quote"><br></div><div class="gmail_quote">This would be roughly analogous to annotation processing: The compiler calls out to plug-ins (implementations of <span style="font-family:monospace">SyntaxProcessor</span><span style="font-family:arial,sans-serif"> say) </span>to evaluate chunks of source code ("templates").</div><div class="gmail_quote"><br></div><div class="gmail_quote">The plug-ins would in turn convert the templates into "recipes" for normal evaluation and pass them back to the compiler. Exactly what these "recipes" are is TBD.</div><div class="gmail_quote"><br></div><div class="gmail_quote">This would look similar to JEP 465 but would be fully agnostic about (a) the literals that define the templates and (b) how they are interpreted.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Simple example equivalent to JEP 465 but with a different (better?) interpolation syntax:</div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace">import com.example.syntax.SQL;</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace">public class MyClass {</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"> public PreparedStatement buildSQL(String name, int age) {</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"> return SQL</span><span style="font-family:monospace">."SELECT * FROM Person WHERE name = :name AND age = :age";</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"> }</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace">}</span></div><div class="gmail_quote"><br></div><div class="gmail_quote">Key points...<ul><li>The syntax for, and meaning of, "interpolation" in the template, if any, is determined entirely by the <span style="font-family:monospace">SyntaxProcessor</span>.</li><li>The Java type of the evaluated template expression can be any Java type (ideally, determined <i>dynamically</i> by the <span style="font-family:monospace">SyntaxProcessor</span><span style="font-family:arial,sans-serif">)</span></li><li>The <i>Literal</i> does not have to be a String, it could be any Java literal. Examples: <span style="font-family:monospace">Currency.'€'</span><span style="font-family:arial,sans-serif">, </span><span style="font-family:monospace">Year.2025</span><span style="font-family:arial,sans-serif">, etc.</span></li><li>The <span style="font-family:monospace">SyntaxProcessor</span> can "access" arbitrary Java expressions, i.e., it can pass them back to the compiler for (eventual) evaluation as part of the "recipe". This facilitates interpolation.</li></ul></div><div>Another simple example demonstrating a totally different "interpolation" that's more appropriate for an XSL transform:</div><div><br></div><div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace">import com.example.xml.XSLT;</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace">import javax.xml.transform.*;</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace">public class MyClass {</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"> public void xmlSplitFullName(Source input, Result output, <span style="background-color:rgb(255,255,0)">String fullNameTag</span>) {</span></div><div class="gmail_quote" style="margin-left:40px"><span style="font-family:monospace"> XSLT</span><span style="font-family:monospace">."""</span></div><div style="margin-left:120px"><span style="font-family:monospace"><xsl:transform xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>" version="2.0"></span><br><span style="font-family:monospace"></span></div><div style="margin-left:120px"><span style="font-family:monospace"><br></span></div><div style="margin-left:120px"><span style="font-family:monospace"> <span style="background-color:rgb(255,255,0)"><xsl:param name="fullNameTag"/><span style="background-color:rgb(255,255,255)"> <!-- "interpolation" happens here... --></span></span></span></div><div style="margin-left:120px"><span style="font-family:monospace"><br></span></div><div style="margin-left:120px"><span style="font-family:monospace"> <xsl:template match="Person/*[name(.) = <span style="background-color:rgb(255,255,0)">$fullNameTag</span>]"> </span><span style="font-family:monospace"><span style="background-color:rgb(255,255,0)"><span style="background-color:rgb(255,255,255)"><!-- ...NOT here --></span></span></span><br><span style="font-family:monospace"> <lastName></span><br><span style="font-family:monospace"> <xsl:value-of select="substring-before(., ', ')"/></span><br><span style="font-family:monospace"> <lastName></span><br><span style="font-family:monospace"> <firstName></span><br><span style="font-family:monospace"> <xsl:value-of select="substring-after(., ', ')"/></span><br><span style="font-family:monospace"> <firstName></span><br><span style="font-family:monospace"> </xsl:template></span><br><span style="font-family:monospace"></span><br><span style="font-family:monospace"> <xsl:template match="@*|node()"></span><br><span style="font-family:monospace"> <xsl:copy></span><br><span style="font-family:monospace"> <xsl:apply-templates select="@*|node()"/></span><br><span style="font-family:monospace"> </xsl:copy></span><br><span style="font-family:monospace"> </xsl:template></span><br><span style="font-family:monospace"></span><br><span style="font-family:monospace"></xsl:transform>""".transform(input, output);</span></div><div style="margin-left:80px"><span style="font-family:monospace">}</span></div><div style="margin-left:40px">}</div><br></div><div>-Archie</div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>
</div>