On 7/25/2023 2:07 AM, Remi Forax wrote:
----- Original Message -----
From: "Tagir Valeev" <[email protected]>
To: "amber-spec-experts" <[email protected]>
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.
The quoted text focuses on results, not side effects. (In line with
Brian's "The spirit here is that the template processor is a *function*,
taking some template-shaped ingredients and turning it into a useful
*thing*, that you can then further use or manipulate.")
In contrast, expression statements are all about side effects, not
results (which are discarded).
So, it's odd to read the quoted text and conclude that the _spec_ should
be updated to make template expressions be expression statements. It's
javac that should be updated to make template expressions _not_ be
expression statements. I don't have any reason to think that JLS 14.8 is
erroneous.
Alex