<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Remi Forax" <forax@univ-mlv.fr><br><b>To: </b>"Brian Goetz" <brian.goetz@oracle.com><br><b>Cc: </b>"amber-spec-experts" <amber-spec-experts@openjdk.java.net><br><b>Sent: </b>Monday, April 17, 2023 5:16:25 PM<br><b>Subject: </b>Re: String templates and constant expressions<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><br><br><hr id="zwchr"><div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Brian Goetz" <brian.goetz@oracle.com><br><b>To: </b>"amber-spec-experts" <amber-spec-experts@openjdk.java.net><br><b>Sent: </b>Monday, April 17, 2023 4:05:18 PM<br><b>Subject: </b>Fwd: String templates and constant expressions<br></blockquote></div><div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">This was received on the -comments list. It raises a question which
I realize now was not explicitly discussed with the EG, but which
was considered in internal discussions: can the result of a string
template ever be considered a constant expression? <br>
<br>
At first, one might thing that a string template whose embedded
expressions (and processor) are constant expressions could be a
constant expression. The questioner's motivation is that the
arguments of annotations must be constant expressions, so this would
enrich the set of things we can put in annotations. <br>
<br>
Unfortunately, the bar for "constant expression" in the JLS is quite
high (and even that doesn't necessarily get us to annotation
parameters.) For String constants, the compiler would "just" have
to fold the string at compile time -- but it would need need
permission from the spec to do so, which means we have to specify at
least part of the "constant folding" story for a very narrow and
ad-hoc benefit, and which would likely lead to an RFE tail of "can
you please allow XYZ to be a constant expression". And for types
more interesting than String/int/friends, we'd have to do more --
we'd have to update the specification of annotations to support
types other than the set of types that currently have an encoding in
JVMS 4.7.16. (We explored this for an arguably more valuable use
case -- method references as annotation parameters -- and while
there was nothing wrong with that in principle, there was
significant work in the classfile spec, VM, compiler, and reflection
(and ASM and friends) to plumb it end to end, and this didn't make
the priority cut.) <br>
<br>
So the short answer to the question is "no", and the slightly longer
answer is "you want constant folding, but we're not going to do
constant folding in little ad-hoc bits."</blockquote><br><div>Note that we discussed about introducing method references in annotation for Java 8.<br></div><div>This is now a little simpler because, since 11, we have constant dynamic and we can extend the annotation tag "c" to work with any constants provided by a constant dynamic instead of only "constant classes" (the same way the meaning of the opcode ldc was extended to support constant dynamic).<br></div><br><div>At least for ASM and the reflection, this change does not change the API, only the implementation.<br></div><br><div>This is still a big change, javac and the VM needs to be updated but this is less scary than before constant dynamic was introduced.<br></div><br><div>Rémi<br></div></div></div></blockquote><div><br></div><div>Just to be clear, i'm not asking to make string template constant expression, but just saying that introducing new constant expressions inside annotations is less an issue than it was because we have better tools. <br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><br>
<div class="moz-forward-container"><br>
<br>
-------- Forwarded Message --------
<table class="moz-email-headers-table" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<th valign="BASELINE" nowrap="nowrap" align="RIGHT">Subject:
</th>
<td>String templates and constant expressions</td>
</tr>
<tr>
<th valign="BASELINE" nowrap="nowrap" align="RIGHT">Date: </th>
<td>Sat, 15 Apr 2023 19:46:07 +0200</td>
</tr>
<tr>
<th valign="BASELINE" nowrap="nowrap" align="RIGHT">From: </th>
<td>Jens Lideström <a class="moz-txt-link-rfc2396E" href="mailto:jens@lidestrom.se" target="_blank"><jens@lidestrom.se></a><br></td>
</tr>
<tr>
<th valign="BASELINE" nowrap="nowrap" align="RIGHT">To: </th>
<td><a class="moz-txt-link-abbreviated" href="mailto:amber-spec-comments@openjdk.java.net" target="_blank">amber-spec-comments@openjdk.java.net</a><br></td>
</tr>
</tbody>
</table>
<br>
<br>
Dear experts and observers,<br>
<br>
A recent message in the thread "Revisit the String template
syntax" compared the Spring property annotation syntax with the
proposed String template syntax.<br>
<br>
@Value("${myproperty}")<br>
private String myproperty;<br>
<br>
I have the somewhat vague question: Are there any relation between
the use case of these property annotations and the use case of
string templates?<br>
<br>
The argument of the @Value annotation must be a String constant
expression.<br>
<br>
Can a string template be used as a constant expression? Maybe if
all its arguments are themselves are constants? Could a property
syntax such as Spring's use string templates?<br>
<br>
Could code like the following do anything sensible?:<br>
<br>
@Value("\{someTemplateArg}")<br>
private String someField;<br>
<br>
Maybe like this:<br>
<br>
@Value("\{MyProperties.SOME_PROPERTY_KEY}")<br>
private String someField;<br>
<br>
Maybe there is no relation and the answer is simply "no". But
maybe it worth to think about.<br>
<br>
I don't see constant expressions mentioned in the JEP:
<a class="moz-txt-link-freetext" href="https://openjdk.org/jeps/430" target="_blank">https://openjdk.org/jeps/430</a><br>
<br>
Regards,<br>
Jens Lideström<br>
<br>
PS: This feature in general seems very good to me.<br>
<br>
</div></blockquote></div></div><br></blockquote></div></div></body></html>