Fwd: String templates and constant expressions

Brian Goetz brian.goetz at oracle.com
Mon Apr 17 14:05:18 UTC 2023

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

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

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

private String someField;

Maybe like this:

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: 

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/6937210e/attachment.htm>

More information about the amber-spec-experts mailing list