[External] : Re: String templates
Ron Pressler
ron.pressler at oracle.com
Sat Aug 9 15:00:37 UTC 2025
> On 9 Aug 2025, at 00:22, Archie Cobbs <archie.cobbs at gmail.com> wrote:
>
>
> Because we spend so much time writing Java code to deal with all the non-Java-stuff, it's worth exploring how the language can help make our lives easier.
Sure.
>
> Java's already going in this "data first" direction, with record classes, value classes, the "standard" JSON classes, etc. Those are nice but they are staying very close to the original language. I think it would be OK and not blasphemous for Java to be more flexible and "open minded" at the language lexical level in the service of the "data first" effort also.
Right.
>
> Here's a tiny motivating example that's not revolutionary but hopefully gives a taste of the idea (ignore for a moment the fact that kids don't use XML anymore :)
>
> Document doc = // read XML document
> for (Element child : XPATH."$doc/Users/User") {
> if (XPATH."not($child/favoriteLanguage)") {
> $child.appendChild(XML."<favoriteLanguage>Java</favoriteLanguage>");
> }
> }
> // write out modified XML document
>
> The point is we try to accept and embrace the domain models we're working with, instead of effectively saying that everything must always be fully converted to Java before we can work with it. In the example, it appears as if you're working with XPATH and XML directly, which probably more closely matches how you really think about the problem. The mashed-up syntax is actually intuitive. In other words, whenever I'm writing Java code that manipulates an XML document, I'm thinking about the XML document as a data structure, not the programming language - the language being used to do the manipulation is just a means to an end. Why not make it easy to use the most natural language for that particular problem?
I’m entirely with you, but I don’t see why that means that the *syntax for a template argument* needs to be different for every hosted language. Templates are an interface between Java and a hosted language, where the fragments are in the hosted language while the arguments are in Java. Since the arguments are in Java, why can’t their escape - i.e. the transition from the hosted language back to Java - be universal (i.e. `\{arg}`)?
Just note that not every useful template/fragment in a hosted language is interpretable in the hosted language as-is. E.g. the token 3 in Java could be an integer value, part of a Java string, or part of a Java comment. The processor of the host language cannot always interpret some partial template, or even determine if it’s valid or not, until the full context is available. That’s one of the core points of our design explorations.
— Ron
More information about the amber-dev
mailing list