New candidate JEP: 430: String Templates (Preview)

Attila Kelemen attila.kelemen85 at gmail.com
Wed Sep 21 21:31:06 UTC 2022


Hi,

Though I like the general idea behind this JEP. I have some concerns
with it in its current form:

1. Might be a personal preference, but I find the
`TemplateProcessorExpression . Template` syntax bizarre.
Also, it seems odd that the Java language doesn't care too much about
terseness, but in this very-very
special case, it uses up syntax possibilities (which in general could
hinder future more general syntax shortening).

2. The `TemplatedString` is more complicated than necessary. It is not
obvious by just looking at it, how to
implement a `TemplateProcessor`. You have to carefully read its
documentation to realize that you have to
alternate between two iterators. To allow that, it even (potentially)
needs the 'hack" of empty guard strings.
Aside from looking a bit sketchy, it even makes it inconvenient to
implement a `TemplateProcessor`. In my
opinion, `TemplatedString` should effectively be a list of
`TemplatePart` (of course, it still should exist as a
wrapping abstraction), where `TemplatePart` is a sealed interface with
2 implementations (the
implementations are being as obvious). This would make implementing
`TemplateProcessor` considerably
easier (especially with switch on types). I'm not a compiler writer
(I'm sure they will hate me for this), but
it looks a lot more plausible that this is more optimizable than
alternating between iterators.

3. It is needless to waste (see above) a perfectly good unicode
character, just to allow a find/replace.
And yes, I'm heating with the spacebar.

4. Last time I saw, the compiler generated a new class for each
`TemplatedString` instance, which looks quite
wasteful (maybe it is to avoid copying the most likely very short
list?). Note that, if it was effectively a
`TemplatePart[]`, then - in some cases -, the compiler could even skip
creating the `TemplatedString`
To be fair, I don't find this optimization possibility that likely,
because it would require some special understanding
from the JIT, or the `TemplateProcessor` would require minor hacks.

Attila


Mark Reinhold <mark.reinhold at oracle.com> ezt írta (időpont: 2022.
szept. 20., K, 20:10):
>
> https://openjdk.org/jeps/430
>
>   Summary: Enhance the Java programming language with string
>   templates, which are similar to string literals but contain embedded
>   expressions. A string template is interpreted at run time by replacing
>   each expression with the result of evaluating that expression, possibly
>   after further validation and transformation. This is a preview language
>   feature and API.
>
> - Mark


More information about the jdk-dev mailing list