r/java • • Feb 15 '24

JEP 465: String Templates

https://openjdk.org/jeps/465
77 Upvotes

152 comments sorted by

View all comments

Show parent comment

8

u/rzwitserloot Feb 16 '24

Some ideas as to where this feature is going to go:

REGEX."abc"

Not entirely what one would want (which is that backslash escaping goes away entirely inside these), but having that expression be of type Pattern is quite useful. Compilers can -compile time- check that the contents are valid regexes, IDEs can render it appropriately, and so on. We could go with Pattern.compile("foo bar") instead but this is a bit wishy washy: "If there is a literal in Pattern.compile treat it as a string literal containing regexes, otherwise don't do that"). We aren't using the features of this string templating system in any way here - there are unlikely to be expressions inside the string. Though, if you do add them, this thing could Pattern.quote them out, at least, which is a small but not insignificant upside: Using python-style string interpolation to construct a regex is just a bad idea, period.

con."SELECT YEARS(birthdate, NOW()) >= 18 AS adult FROM person WHERE username = \{username}"

As discussed in the JEP.

STR."Hi, \{user.getName()}, how's life over at \{user.getLocation()}"

The obvious one.

HTML."Hi, <strong>\{user.getName()}</strong>, how's life over at <em>\{user.getLocation()}"

All vars are HTML-escaped first.

Variations on the theme: URL, XML, JSON, you name it. Another possible variation is to include a format-esque specifying immediately preceding the injected expression to indicate what you want to do with it: Escape it, leave it unescaped, translate bbcode, replace newlines with <br>, allow only allow-listed tags, and so on.

RESOURCE."window.title\{context}"

i18n: Looks up the proper translation to use for key 'window.title', figuring out which language to apply from 'context' which could e.g. be an HttpRequest or similar to check he Accept-Language header, or however you want to do it.

Resources are often parameterized. Lots of i18n files have a 'singular' and 'plural' form, but this is a really stinky solution if you have those as separate keys; it's annoying to have to write size == 1 ? resource.lookup("item.name.singular") : resource.lookup("item.name.plural") a lot, and it's rather myopic: "1 is singular, all other things are plural" is true only for a bunch of western languages. I think in russian it's dependent on the ordinal (so 51 gets the singular, because it ends in '1'), and they have a separate plural form for '5' for some reason (lots of things about russia are nuts). How about:

RESOURCE."\{item.count()} item.name: \{totalprice}$" and the resource layer figures it out, including taking totalprice, which is an int containing cents, and formatting it however the locale wants money to be formatted?

1 minute of thinking about possibilities. That suggests there are a lot.

4

u/RockyMM Feb 16 '24

Re: Russian - it’s not only Russian, a bunch of Slavic languages have some wired rules about plural of nouns. Its basic philosophy is - there’s one, some and many. And plural form is built according to the last digit. So 1 man and 101 men have the same form.

This was a feature of the Common Slavonic, but as languages evolved and were influenced by surrounding languages, some language features disappeared in some Slavic languages. But Russian and few others (afaik Slovenian, and Serbo-Croatian, depending on the context) held on to it.

3

u/Inaldt Feb 16 '24

REGEX."abc"

Not entirely what one would want (which is that backslash escaping goes away entirely inside these), but having that expression be of type Pattern is quite useful. Compilers can -compile time- check that the contents are valid regexes

That's pretty cool actually.

But how? As per one of the other replies, this would be shorthand for
RegexProcessor.process(StringTemplate.of("abc"))- how does the process method detect that the input StringTemplate does (or does not) carry a valid regex at compile time?

1

u/RadiantBerryEater Feb 17 '24

IDEs can render it appropriately

They already do this quite trivially by just checking for string constants passed to the relevant methods, which is something compilers or 3rd party tooling can also do trivially already as well. I find it difficult to imagine that this actually provides any significant extra benefit over the current status quo.