Revisit the String template syntax

forax at univ-mlv.fr forax at univ-mlv.fr
Sat Apr 15 08:28:15 UTC 2023


----- Original Message -----
> From: "Tagir Valeev" <amaembo at gmail.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Guy Steele" <guy.steele at oracle.com>, "Brian Goetz" <brian.goetz at oracle.com>, "amber-spec-experts"
> <amber-spec-experts at openjdk.org>
> Sent: Saturday, April 15, 2023 9:48:14 AM
> Subject: Re: Revisit the String template syntax

> Hello,  Rémi!
> 
>> JavaScript is a user of the ${} syntax but it's a fairly recent addition.
>>
>> In Java, Maven, Gradle through their lineage to Apache Ant, in fact, all
>> projects using the apache common-text StringSubstitutor (or the much older
>> StrSubstitutor) are using the ${} syntax since a very long time. All Java
>> developers are familiar with this syntax for string interpolation.
>>
>> So for a lot of Java developers, the syntax \{} feels alien to Java in
>> comparison. So yes, you have to escape the $ sign, you have to tweak the lexer
>> but, in my opinion, it's a case where familiarity in the whole ecosystem trumps
>> local rationalization.
> 
> To me, this is actually an argument against ${}. With \{}, the users
> will have a clear difference between language-supported interpolation
> and external library interpolation. I can imagine that in some cases,
> you may need to use both. For example, imagine that you are generating
> build.xml or pom.xml and want to store strings containing ${...} as is
> without interpolating right now, because they refer to the properties
> defined in that file. However, you want to interpolate eagerly some
> other parts of these strings using Java string interpolation.
> Something like STR."${project.basedir}/build/\{generateTempDirName()}/".
> You can easily combine two approaches with \{} escapes. It would be
> much harder and much more error prone to create such templates if the
> same syntax was used for both Java and external interpolation.

It's not really harder, you escape the first dollar-sign,
  STR."\${project.basedir}/build/${generateTempDirName()}/"

> I think that distinguished interpolation syntax, which is rarely used in other
> contexts, is actually an advantage. People will get used to it.

The real question is more, do we want two syntax for conceptually the same thing or not ?

Here is an example with an annotation,

  @Bean
  class SpringBean {

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

      public void foo() {
          System.out.println(STR."myproperty: ${myproperty}");
      }
  }


> 
> With best regards,
> Tagir Valeev

regards,
Rémi



More information about the amber-spec-experts mailing list