Update on String Templates (JEP 459)

Robbe Pincket robbepincket at live.be
Fri Mar 15 00:24:11 UTC 2024


On 14/03/2024 23:44 UTC, Maurizio Cimadamore wrote:
           On 14/03/2024 22:36, Robbe Pincket wrote:
                      (I’m not a big fan of `TEMPLATE"Foo: \{bar}"` either as it’s just so much longer than `"Foo: " + bar`)

           Note that when I suggested TEMPLATE as a prefix I was obviously not being super serious :-)

           Let's do a test (bear with me). Let's assume the two prefixes were S and T (not saying I like them, just trying them out for size). Let's also assume there's no conversion. Then your examples become:

           ```
           String s1 = "test" // still a string literal
           StringTemplate st2 = T"test" // allowed, constant strings can be implicitly converted to templates
           StringTemplate st3 = "Foo: \{bar}" // Simple string template
           String s4c = S"Foo: \{bar}" // short for String.of("Foo: \{bar}")
           ```

           I think that's not too bad? (please don't focus too much on the letters).
           In the sense: the rare cases (st2) has a prefix. And the operation we want explicit (s4c) also has a prefix. Everything else is fine.

So the difference is that `T` (or something else) has to be used for templates without any holes?
To me it feels a bit weird to have a prefix for the special case of a hole-less template.

I think I saw an argument passing by, saying something along the line that `String` and `StringTemplate` are semantically different so implicit conversion in either direction would be bad because it would be ambigous.
If this `T` idea is based on that, I don't really see why it would be that bad. If an API accepts either, I would intuitivly expect that passing a string and passing a hole-less template with the same string would give me the same result.

           Control question #1: does the conversion here change things much? Or, are we reaching for conversions just to have something "shorter" ?
           Control question #2: let's now assume that S and T were spelled (String) and (StringTemplate), respectively. How do we feel about this?

           ```
           String s1 = "test" // still a string literal
           StringTemplate st2 = (StringTemplate)"test" // allowed, cast from constant string to template
           StringTemplate st3 = "Foo: \{bar}" // Simple string template
           String s4c = (String)"Foo: \{bar}" // allowed, cast from template back to String (interpolation)
           ```

I think I answered #1. Having the 'T' *just* for the "hole-less" template feels a bit odd? I don't you can sell me on #2.
* Why would I prefer using `(String)"Foo: \{bar}"` over `"Foo: " + bar`. This is not just a "length issue", as templates would win with more holes, but there is also a cost of switching habits.
* (Ignoring primitives), casts have up until now always just returned the input, but now with a different static type. Using the casting syntax to do actual interpolation (or create a template from a string) feels weird to me

If ´(StringTemplate)"test"` and `(String)"Foo: \{bar}"` are valid, will the following things work too? `"test" instanceof StringTemplate template` and `"Foo: \{bar}" instanceof String str`. The second one I'd assume no, the first one is a bit unclear to me.

           Maurizio

Kind regards
Robbe Pincket
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20240315/70f4d2dc/attachment.htm>


More information about the amber-spec-observers mailing list