[string-templates] Template expression in expression statement?

Tagir Valeev amaembo at gmail.com
Tue Jul 25 08:52:04 UTC 2023


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) {

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.

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