News for hackers. Stuff that matters.
Re:Java 21 Released by jaimehrubiks on Tuesday September 19, 2023 @10:22AM (#37570385)
String templates seems like a basic needed and welcome feature nowadays
Re:Java 21 Released by owlstuffing on Tuesday September 19, 2023 @10:33AM (#37570544)
A bit late to the party, but yes. Although, in true Java form the designers sacrificed readability to satisfy lesser use-cases.

STR.”Total: \{count}”

Could not get uglier.

Re:Java 21 Released by uzername on Tuesday September 19, 2023 @10:39AM (#37570632)
I haven't tried it yet but I'm guessing this will fold into a different visual representation in IntelliJ when the cursor isn't nearby. It's not pretty but if it's consistent I guess it's fine.
Re:Java 21 Released by owlstuffing on Tuesday September 19, 2023 @10:43AM (#37570676)
I’m using project manifold’s string templates[1] and loving it.

1. https://github.com/manifold-systems/manifold/tree/master/man...

Re:Java 21 Released by avgcorrection on Tuesday September 19, 2023 @11:09AM (#37571015)
> I haven't tried it yet but I'm guessing this will fold into a different visual representation in IntelliJ when the cursor isn't nearby.

sigh

Re:Java 21 Released by freedomben on Tuesday September 19, 2023 @10:42AM (#37570662)
Eww, you're not kidding, that's awful.

Java is my most ambivalent language. There's things I love, and things I hate, and when it's good it's really good, when it's bad ... Yuck.

That said, I do think Python went worse with f-strings. Would have loved to see Java look to Groovy for inspiration.

Re:Java 21 Released by ryangs on Tuesday September 19, 2023 @10:43AM (#37570673)
It looks so bad in the context they give themselves:

C# $"{x} plus {y} equals {x + y}"

Visual Basic $"{x} plus {y} equals {x + y}"

Python f"{x} plus {y} equals {x + y}"

Scala s"$x plus $y equals ${x + y}"

Groovy "$x plus $y equals ${x + y}"

Kotlin "$x plus $y equals ${x + y}"

JavaScript `${x} plus ${y} equals ${x + y}`

Ruby "#{x} plus #{y} equals #{x + y}"

Swift "\(x) plus \(y) equals \(x + y)"

And finally:

Java STR."\{x} plus \{y} equals \{x + y}"

Re:Java 21 Released by hibikir on Tuesday September 19, 2023 @10:51AM (#37570761)
It's explained if you look at the proposals: They wanted to avoid any and all risks of backwards compatibility with any existing templating tool in the Java ecosystem. Every sensible syntax was covered by one of those, so the core language is stuck with the least bad one available.
Re:Java 21 Released by _old_dude_ on Tuesday September 19, 2023 @11:07AM (#37570989)
It's prefixed by "STR." so the syntax is different from any existing tools ?

It remember me the first proposed syntax for lambdas. Let's hope the next preview will fix that.

Re:Java 21 Released by paulddraper on Tuesday September 19, 2023 @10:57AM (#37570832)
...at what cost tho
Re:Java 21 Released by capableweb on Tuesday September 19, 2023 @11:32AM (#37571398)
The JEP makes it kind of clear:

> trading off a small amount of convenience to gain a large amount of safety

Re:Java 21 Released by paulddraper on Tuesday September 19, 2023 @11:50AM (#37571716)
*large

*small

Re:Java 21 Released by bcrosby95 on Tuesday September 19, 2023 @12:43PM (#37572570)
You can create json and sql string template processors that suffer from no injection problems. I'd call that a large improvement over, for example, Javascript. But I'm not familiar enough with the string template feature in the other languages to comment on their relative security.
Re:Java 21 Released by paulddraper on Tuesday September 19, 2023 @01:08PM (#37572978)
> You can create json and sql string template processors that suffer from no injection problems. I'd call that a large improvement over, for example, Javascript.

Bad example. JavaScript literally has that. [1]

  function sql(strings, ...args) {
    // ...
  }

  sql`SELECT * FROM user WHERE email = $1`
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re:Java 21 Released by 5e92cb50239222b on Tuesday September 19, 2023 @01:01PM (#37572873)
This is a solved problem in .NET, and C# only has one kind of string templating:

  FormattableString foo = $"select * from {table} where {column} = 42";
https://learn.microsoft.com/en-us/ef/core/querying/sql-queri...
Re:Java 21 Released by kaba0 on Tuesday September 19, 2023 @01:34PM (#37573349)
Not sure if C# can do that, but java’s implementation can return an arbitrary object based on the templates/parameters.
Re:Java 21 Released by ivan_gammel on Tuesday September 19, 2023 @11:05AM (#37570957)
As a reminder, you still can make it $.”…” in your code, if you wish.
Re:Java 21 Released by samus on Tuesday September 19, 2023 @07:40PM (#37578604)
It makes perfect sense if you consider that String Templates don't just support interpolation, but also /escaping/ of values. Therefore, Java's existing syntax for escaping stuff is the obvious choice for consistency reasons. If interpolation is all you want, use `STR`. In the future, we will see libraries with template processors for regex patterns, SQL, XML, etc.