String templates and constant expressions

Remi Forax forax at univ-mlv.fr
Mon Apr 17 15:24:25 UTC 2023


> From: "Remi Forax" <forax at univ-mlv.fr>
> To: "Brian Goetz" <brian.goetz at oracle.com>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Sent: Monday, April 17, 2023 5:16:25 PM
> Subject: Re: String templates and constant expressions

>> From: "Brian Goetz" <brian.goetz at oracle.com>
>> To: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
>> Sent: Monday, April 17, 2023 4:05:18 PM
>> Subject: Fwd: String templates and constant expressions

>> 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?

>> 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.

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

>> 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."
> Note that we discussed about introducing method references in annotation for
> Java 8.
> 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).

> At least for ASM and the reflection, this change does not change the API, only
> the implementation.

> 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.

> Rémi

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. 

Rémi 

>> -------- Forwarded Message --------
>> Subject: 	String templates and constant expressions
>> Date: 	Sat, 15 Apr 2023 19:46:07 +0200
>> From: 	Jens Lideström [ mailto:jens at lidestrom.se | <jens at lidestrom.se> ]

>> To: 	[ mailto:amber-spec-comments at openjdk.java.net |
>> amber-spec-comments at openjdk.java.net ]

>> Dear experts and observers,

>> A recent message in the thread "Revisit the String template syntax" compared the
>> Spring property annotation syntax with the proposed String template syntax.

>> @Value("${myproperty}")
>> private String myproperty;

>> 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?

>> The argument of the @Value annotation must be a String constant expression.

>> 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?

>> Could code like the following do anything sensible?:

>> @Value("\{someTemplateArg}")
>> private String someField;

>> Maybe like this:

>> @Value("\{MyProperties.SOME_PROPERTY_KEY}")
>> private String someField;

>> Maybe there is no relation and the answer is simply "no". But maybe it worth to
>> think about.

>> I don't see constant expressions mentioned in the JEP: [
>> https://openjdk.org/jeps/430 | https://openjdk.org/jeps/430 ]

>> Regards,
>> Jens Lideström

>> PS: This feature in general seems very good to me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20230417/acc29382/attachment.htm>


More information about the amber-spec-experts mailing list