[External] : Re: String templates

Chris Bouchard chris at upliftinglemma.net
Sat Aug 9 19:49:42 UTC 2025


On Sat, Aug 9, 2025, 13:45 Archie Cobbs <archie.cobbs at gmail.com> wrote:

The only answer to that question I can think of is that many languages
> already have a lexical notion of a parameter or variable and don't need a
> new one.
>
> For example, it would be cleaner to write  XPATH."($param/foo[@myattr])" instead
> of  XPATH."\{param}/foo[@myattr]".
>

The problem, in my opinion, is that the only common denominator with
templates is that we're writing Java. How does a code reviewer for this
snippet know that XPath uses $ to introduce variable references? What about
the @? Is that one too? From the reviewer's point of view, there's no
syntactic indication that there are *any* variable references here, because
the only syntax we can assume they know is Java syntax.

To truly emulate embedded language roles for variable references, each
template processor would need to define its own bespoke, unpredictable,
potentially context-sensitive rules for variable references. Consider a
language like Bash, where an unbraced variable reference ends at the first
character that is not valid in a variable name.

I think my expectation for templates would be that, as long as the reviewer
knows the *template system*—not the individual template processors, but the
syntax for using templates—they should be able to answer those questions.
It feels untenable to force reviewers to research individual template
processors for questions as basic and security-sensitive as, "Is this
template capturing PII?"

But also, you could imagine a language template like

    final int x = 42;
    var e = JAVASCRIPT."() => { return x; }";

that creates a JSExpression object, which can be passed to a hosted
JavaScript runtime for evaluation. In JavaScript, that x is referencing a
variable that has not been declared lexically, so it could capture a
variable in the outer scope. Does that template capture the Java variable x
declared above? Or would it use the global x variable in my hosted
JavaScript runtime? Neither option is obviously correct, and both are
limiting.

If that syntax does capture the x from Java, how do I disable that so I can
reference the JavaScript global? That's maybe not obviously useful for x,
but what about variables like String or Function (since those are variables
in JavaScript).

And if that syntax doesn't capture the x from Java, then how would I
express that when there's no JavaScript syntax for "reference this variable
outside the runtime."

Both options require the template implementation to invent invent an escape
syntax that JavaScript doesn't have. This isn't a problem just for
JavaScript—how many languages with variable references also have a syntax
for escaping that reference to some unknown outer host score? So why not
just use a standard Java escape syntax instead?

And finally, using in-language syntax for variable references precludes
embedding anything more interesting that a variable. For example, compare

    JAVASCRIPT."{ a: \{obj.x}, b: \{obj.y}, c: \{obj.z} }"

with

    var x = obj.x;
    var y = obj.y;
    var z = obj.z;
    JAVASCRIPT."{ a: x, b: y, c: z }"

The latter is more verbose and repetitive, and introduces an extra layer of
indirection to know what's being used in the expression—all to avoid typing
\{}.

I'll say, I can see the appeal of language-specific sub-syntaxes from the
point of view of an author who is familiar with each template processor
they're using. But I don't feel like they're a big enough win for the cost
in understanding for everyone else.

Chris Bouchard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250809/ca59fa1f/attachment-0001.htm>


More information about the amber-dev mailing list