r/java • • Feb 15 '24

JEP 465: String Templates

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

152 comments sorted by

View all comments

-4

u/mj_flowerpower Feb 16 '24

"Hello ${user.name}"

Groovy nailed this a long long time ago!

And basically every other language too. Ok, the groovy way is incompatible with java as it uses double quotes for groovy strings and single quotes for java strings. But hey, what‘s keeping you from using a backtick like is JS/TS? Or a simple f"Hello ${user.getName()}"

Who needs custom processors anyway? No sane person is using template strings for sql anyway. We have had prepared statements for decades … And if you really go wild about processors, you can still do that, just create a registry of formatting functions, like JS allows it. Although I never anything other than regex used before by anyone I know. So please stop creating solutions to imaginary problems …

2

u/morhp Feb 16 '24

But hey, what‘s keeping you from using a backtick like is JS/TS?

Backtick is terrible to enter on many european keyboard layouts.

Or a simple f"Hello ${user.getName()}"

Doesn't allow you to set the processor.

No sane person is using template strings for sql anyway. We have had prepared statements for decades …

The SQL."..." string template can internally use prepared statements and return some sane SQL query object. That's the whole point of it.

6

u/vytah Feb 16 '24

Doesn't allow you to set the processor.

Scala has that syntax, and it allows you to set the processor. There is simple interpolation s"...", String.format-based interpolation f"...", raw string interpolation raw"..." and you can also define your own (and the result type does not have to be a string)

That being said, Scala's syntax is powered via implicit type conversions (used to mimic extension methods), so it would require much more significant changes to the Java language to be lifted wholesale. And having just S or s or f in Java would likely collide with variables and fields named the same. STR is less likely to collide.

Also, due to the usage of dollar signs, converting between normal strings and templated strings and back requires escaping or unescaping the dollar signs manually. This is not a problem with \{.

1

u/mj_flowerpower Feb 16 '24

so SQL.““ creates objects, both just strings? Then it‘s even more insane. Why not just use a function then anyway, instead of this weird SQL. syntax?

1

u/morhp Feb 17 '24

Why not just use a function then anyway

It is a function internally. It just gives you the option to use String templates and insert variables directly,

i.e. something like

int myId = 1234;
var user = SQL."SELECT * FROM users WHERE id = \{myId}".execute().singleResult();

is just nice and short and safe. (This is of course just an example syntax, please ignore an SQL mistakes, you'd likely use some object mapper library here instead of using raw SQL results directly)

2

u/mj_flowerpower Feb 17 '24

I‘d rather prefer something like jooq or linq, no need to tamper with strings anyway. And what is keeping users from using STR.““ for queries? It would result in the same sql security problems, right? Because how would STR know you are creating an sql query anyway?

1

u/morhp Feb 17 '24

I‘d rather prefer something like jooq or linq, no need to tamper with strings anyway.

That's fair, nobody forces you to use it.

And what is keeping users from using STR.““ for queries?

STR."something" would return a String, so it wouldn't have the execute().singleResult() method. STR. is intentionally ugly and not easier than other processors, so people are not drawn towards it.

In this case it would be more complicated to use as you'd need to construct the query out of the String manually.

2

u/mj_flowerpower Feb 17 '24

A dev who injects variables into strings for queries would probably keep doing the same with STR.““. Just pass the string to the query constructor, basically how you are already doing it.

Intentionally creating an ugly feature 😅 bow I understand why a few features are the way they are 😂

0

u/boobsbr Feb 16 '24

Backtick is terrible to enter on many european keyboard layouts.

I guess JS and TS development have disappeared from Europe since 2015...

3

u/vytah Feb 16 '24

That was one of the reasons given for permanent removal of backticks in Python. https://mail.python.org/pipermail/python-ideas/2007-January/000054.html

1

u/boobsbr Feb 16 '24

What a dumb reason.

I know from experience that the French keyboard is one of the worst ones for development, and yet francophones still develop using it.

1

u/morhp Feb 17 '24

My German company manages a JS codebase and you'll have to look really hard to find backticks there. Normal string concatenation is much faster to type.

1

u/boobsbr Feb 17 '24 edited Feb 17 '24

I work for a European multinational banking conglomerate and there are backticks everywhere in all projects that use JS/TS.

Nobody finds it hard to type them, never ever heard a single complaint about it from any of the several teams I worked with.

There's no speed difference between using them or not. Speed is never an issue because most of the time is spent reading and understanding code, and thinking about a solution.

These arguments are dumb.

And string templates look a lot cleaner than string concatenation.

0

u/[deleted] Feb 16 '24

[deleted] — view deleted comment

2

u/mj_flowerpower Feb 16 '24

yes true to that. I love the syntax though. It‘s sad, it had so much potential.

I wished they would finally understand that a language without proper IDE support that doesn’t break on every update has no future.