[string-templates] Template expression in expression statement?

Remi Forax forax at univ-mlv.fr
Tue Jul 25 09:07:42 UTC 2023

----- Original Message -----
> From: "Tagir Valeev" <amaembo at gmail.com>
> To: "amber-spec-experts" <amber-spec-experts at openjdk.org>
> Sent: Tuesday, July 25, 2023 10:52:04 AM
> Subject: [string-templates] Template expression in expression statement?

> Hello!
> It looks like, the latest javac (build 22-ea+7-489) accepts string
> templates as expression statements:
> public class Demo {
>    public static void main(String[] args) {
>        STR."hello";
>    }
> }
> The expression statement spec (JLS 14.8, [1]) states that only
> 'StatementExpression' productions are allowed inside expression
> statements. This is a closed list of expressions, and it was not
> updated in the latest spec draft for JEP 430 [2] to include 15.8.6
> Template Expressions. I'm not sure what the latest consensus is. On
> one hand, template expression is essentially a method call that may
> produce a side effect (e.g. logging :D). On the other hand, such kind
> of usage is probably discouraged. In any case I see the discrepancy
> here: either spec or javac implementation should be updated.

The JEP contains these sentences
"However, it is unwise for a template processor to trigger potentially long-running actions in order to compose a result. It is also unwise to embark upon actions that can have side effects, such as updating a database. The authors of template processors are strongly advised to focus on validating their input and on composing a result that gives maximum flexibility to the client."

so I believe the spec needs to be updated.

> With best regards,
> Tagir Valeev.


> [1] https://docs.oracle.com/javase/specs/jls/se20/html/jls-14.html#jls-14.8
> [2]
> https://cr.openjdk.org/~gbierman/jep430/jep430-20230526/specs/string-templates-jls.html

More information about the amber-spec-experts mailing list