Update on String Templates (JEP 459)
Clement Cherlin
ccherlin at gmail.com
Thu Mar 14 20:53:09 UTC 2024
On Thu, Mar 14, 2024 at 2:24 PM Maurizio Cimadamore
<maurizio.cimadamore at oracle.com> wrote:
>
> On 14/03/2024 19:04, Clement Cherlin wrote:
>
> What if instead of the extremes of a myriad of processors, or a single
> template prefix, or no prefix and complex/confusing context rules, we
> have exactly two prefixes? To avoid bikeshedding (obviously, the final
> names would be much shorter), I will call them TEMPLATE and
> INTERPOLATE. These are semantically identical to the old RAW and STR
> processors respectively, but syntactically have no "." between them
> and the leading quote.
>
> TEMPLATE"hey \{name}" -> StringTemplate
> INTERPOLATE"hey \{name}" -> String
>
> See my latest email to Guy.
>
> Having different prefix for interpolated vs. raw template literals does help a bit with the case I brought up there - as here we’re basically in a world where a string literal with embedded arguments must have a suitable prefix.
Yes, that's what I had in mind when writing this proposal.
> A possible point which is not too far from where we are today is just reuse STR and RAW as prefixes,
I considered that, but while I have no problem with STR, I think the
name RAW is too much a legacy of the Processor API. I think RAW should
be replaced by something that signifies "this is a template", not
"process this template with the processor that does nothing". I will
continue to resist the urge to present alternative prefixes until the
appropriate time.
> but also make RAW optional, so that:
>
> it can be used to disambiguate interpretation of strings w/o embedded expressions;
>
> it can be the default if you type something that does have embedded expressions (e.g. nudge towards the safer route if there’s embedded expressions)
That's certainly an option. I don't prefer it, but it's not terrible,
and would reduce clutter somewhat when you are dealing exclusively
with templates and don't need the reminder. Like "final" on an
effectively final value, it could be a matter of taste and convention
whether to include the prefix for a template with embedded
expressions. However, it has the downside that you may accidentally
convert a template back to a string literal by removing the last
embedded expression.
> Another idea that came up was, instead of just using prefixes, use types:
>
> the STR prefix is written String
> the RAW prefix is written StringTemplate
>
> This is slightly better (say what you mean!) - but a potential problem is that one might wonder why a special syntax is needed given a cast is just a pair of ( and ) away…
>
> Maurizio
I'm going to assume you're joking here, so I don't feel the need to
write a thousand words about how terrible Java's casting syntax is.
Cheers,
Clement Cherlin
More information about the amber-spec-observers
mailing list