Updated spec for String Templates (JEP 430) now available

Gavin Bierman gavin.bierman at oracle.com
Thu Nov 17 10:37:25 UTC 2022


Thanks Brian:

> On 16 Nov 2022, at 16:21, Brian Goetz <brian.goetz at oracle.com> wrote:
> 
> The spec seems fuzzy on how we perform the equivalent of indentation adjustment from text blocks.  Suppose I have
> 
>     String s = STR."""
>         Hello \{name}, it is \{time}
>     """;
> 
> This should result in the string "Hello Bob, it is 3:00".  My understanding of how it should work is something like:
> 
>  - replace all the embedded expressions with a placeholder character;
>  - evaluate the resulting text block;
>  - split the text block at the placeholder to produce the fragment literals.
> 
> But the spec says only:
> 
>> A template resembles a string literal or a text block but consists of the strict alternate interleaving of two or more string literals or text blocks, known as fragment literals, with one or more embedded expressions. An embedded expression can be either empty or an expression.
> 
> What is being interleaved is neither string literal nor text block, so the use of "string literal" and "text block" seems off here, and I don't see how the TextBlockTemplateFragment is processed for escapes and whitespace the way we would with a TextBlock.  So it seems a little work is needed here?

This needs some work, agreed, but what we are attempting here is a high-level description of what a template is, not its syntactic representation. An analogy might be how we talk about arguments in a method invocation. We just talk about "the arguments", and evaluating “the arguments”. We don’t talk about there being a “thing" starting with a left bracket containing expressions separated by commas etc. 

There is some non-normative text about how the grammar is intended to allow you to think of double-quote followed by string fragment followed by escaped expression etc as if it were a string literal followed by expression … If you pull on this thread you end up saying “oh this template denotes an alternating sequence of strings and expressions”. Perhaps “denotes” is better than “consists of”?

Hope that helps? But you’re right - this section needs more work...

Thanks,
Gavin

PS Just to share the pain: The problem with your description about the Hello Bob example is that it is a template expression. So, what’s that thing on the right? It’s not a literal, clearly. Do you want to say it’s an expression? If you do that then we have to unpick most of chapter 15, as it’s not a general expression - we don’t want you to use it anywhere else. The JLS works by making compile-time statements and run-time statements. Your statement about “how it works” is clearly run time. What about compile time? What is it? The concrete reality is that it is sugar for a constructor call with the strings and embedded expressions that the parser has pulled out for you. But we don’t want to mandate anything about how any of this might be implemented - right? - so we have to make the description more abstract...



More information about the amber-spec-observers mailing list