News for hackers. Stuff that matters.
Re:Java String Interpolation final in Java 23 by sitta on Saturday January 13, 2024 @09:22AM (#38980143)
The comments about this feature are always so tiresome. Please just scroll down and read the "Alternatives" section at the bottom. They're not idiots. They're working within backwards compatibility constraints. They can't just do it like other languages.
Re:Java String Interpolation final in Java 23 by david_allison on Saturday January 13, 2024 @09:29AM (#38980220)
C# introduced `#nullable enable` to change the behaviour of reference types.

Java could do the same to change the behaviour of `$` inside a string (along with a project-level config to make this the default).

Let developers opt-in to better syntax, rather than using significantly worse syntax to support legacy code

https://learn.microsoft.com/en-us/dotnet/csharp/nullable-mig...

Re:Java String Interpolation final in Java 23 by ghusbands on Saturday January 13, 2024 @10:21AM (#38980718)
It's moot, anyway, as parser changes are already required by the STR." part. About the only possible defence is that one could use the same lexer, but even that's not true because you can presumably now embed a multi-line string in a single-line string, like so:

  STR."\{STR."""
  \{hey}
  """}"
Re:Java String Interpolation final in Java 23 by vlakreeh on Saturday January 13, 2024 @09:34AM (#38980264)
Even with their constraints they could have made this nicer, especially around the default formatter. Always requiring an explicit formatter is a classic example of Java being unnecessarily verbose, `\{ ` already wasn't permitted by previous java compilers as it was an unknown escape sequence so we could have nicer templates for the stock formatting like "\{foo} bar".
Re:Java String Interpolation final in Java 23 by rco8786 on Saturday January 13, 2024 @09:26AM (#38980184)
All super weak arguments IMO. Problems that somehow every other language doesn’t have?
Re:Java String Interpolation final in Java 23 by marginalia_nu on Saturday January 13, 2024 @09:41AM (#38980333)
Other languages are not attempting to be backwards compatible with Java.
Re:Java String Interpolation final in Java 23 by rco8786 on Saturday January 13, 2024 @09:46AM (#38980375)
One of the arguments is that existing $ in strings would have to be escaped. Every other language supported $ in strings before interpolation appeared. Not Java specific.

Every other language is susceptible to SQL injection style attacks. Not Java specific.

Re:Java String Interpolation final in Java 23 by marginalia_nu on Saturday January 13, 2024 @10:23AM (#38980740)
Few other languages are as committed to backwards compatibility as Java though.
Re:Java String Interpolation final in Java 23 by wiseowise on Saturday January 13, 2024 @09:36AM (#38980289)
Super weak how? They're quite reasonable and weighed-out.
Re:Java String Interpolation final in Java 23 by _old_dude_ on Saturday January 13, 2024 @09:52AM (#38980451)
You can not use the excuse of the backward compatibility twice.

If the string template is prefixed by a processor STR."foo" instead of "foo" then you do not need \{variable} instead of $variable.

Re:Java String Interpolation final in Java 23 by jitl on Saturday January 13, 2024 @11:06AM (#38981189)
They want to ensure the new interpolation sequence is a compiler error in non-template strings, so that the incorrect “hi \{name}” is an error instead of silently doing the wrong thing. Using \{ accomplished this because it’s an unknown escape sequence in older Java (so forbidden there), and in new Java will have the more specific error of “yo you forgot the template processor at the start of your template”.
Re:Java String Interpolation final in Java 23 by _old_dude_ on Saturday January 13, 2024 @11:33AM (#38981525)
Yes, you do not get a compile time error, but visually a big STR is front is enough "${variable}" vs STR."${variable}"

And editors will use different colors.

Using \ or $ is not about backward compatibility but about ergonomics.

Re:Java String Interpolation final in Java 23 by usrusr on Saturday January 13, 2024 @12:32PM (#38982264)
You don't want friction on the default path to be too low because that would inevitably increase the occurrence of default path where it must not be used, e.g. cause sql injections.

Much easier to accidentally write something that boils down to

   "\{bobby}" 
where it should have been

   SQL."\{bobby}"
than

   STR."\{bobby}"
instead of

   SQL."\{bobby}"
(i admit that this argument would work better if STR was typographically more different from the name you'd inevitably use for an SQL statement processor)
Re:Java String Interpolation final in Java 23 by sergiotapia on Saturday January 13, 2024 @09:39AM (#38980318)
I think most people read the whole thing. I have. It's still very ugly
Re:Java String Interpolation final in Java 23 by troupo on Saturday January 13, 2024 @09:40AM (#38980325)
Those are extremely weak arguments.

Elixir has string interpolation and custom "templates" or whatever they are called without the ugly syntax:

  ~h"custom one"
  ~s"custom two"
etc.
Re:Java String Interpolation final in Java 23 by jitl on Saturday January 13, 2024 @11:07AM (#38981199)
~x”hi” is basically the same amount of ugly to me as X.”hi”
Re:Java String Interpolation final in Java 23 by troupo on Saturday January 13, 2024 @12:38PM (#38982362)
I find it significantly less jarring than STR."hi"
Re:Java String Interpolation final in Java 23 by ioasuncvinvaer on Saturday January 13, 2024 @09:47AM (#38980388)
How are these interpolated?