JEP 430 String Templates, some weird scenarios feedback

Stephen Colebourne scolebourne at joda.org
Sat Jun 17 10:02:31 UTC 2023


On Sat, 17 Jun 2023 at 01:03, Brian Goetz <brian.goetz at oracle.com> wrote:
>
> More generally, any variable that is today used in string concat, String::format, or println is a candidate for use in a template.  Many, many of these will not be effectively final.

OK the logging example is reasonable. And the OP used `interpolate()`
directly which is non-standard. Maybe the messaging needs to be that
holding onto an instance of the template is dubious outside of
frameworks? Having an accessible template implies you can do something
useful with it, when really the best use for it is to pass to a
framework-level method..


Having reviewed it more, I think my real issue here is different - the
"template" terminology is very misleading to me. IMO it is NOT a
template at all, as everything is captured once. A template is a
"thing with holes", where you fill in the holes before final use.
There is no `withValues(List)` method to swap the values that I can
see. So I struggle to see how it can be called a "template".

The API design seems to have two concepts smooshed together - a proper
template, and a "bound template".

 // alternative API design - StringTemplate only contains the fragments
 // BoundStringTemplate is equivalent to StringTemplate today
 StringTemplate st = StringTemplate.parse("Hello \{} \{}");
 BoundStringTemplate bt = st.bind("dear", "friend);
 String result = processor.process(bt);
 | Hello dear friend

 // syntax sugar
 String s = STR."Hello \{a} \{b}";
 String t = StringTemplate.parse("Hello \{} \{}").bind(a, b).process(STR);

Note how this API design does not have to change the Java syntax of
template expressions at all. Nor does it affect the safety or method
handle part of the JEP AFAICT. Note how the syntax sugar explanation
makes it obvious that `bind()` captures the values, not the variables.


Reading the Javadoc, it is unclear why `interpolate()` exists. The
`process()` method appears to be the main standard entry point which
safely produces the expected outcome. The Javadoc could be read to
imply that `interpolate()` just joins the fragments and values
together into a String without validation. Even if it is validated, it
seems odd to offer a way of producing a String if the natural type of
the template is not a String.

Stephen


More information about the amber-dev mailing list