News for hackers. Stuff that matters.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Friday March 03, 2023 @02:34PM (#35013991)
We could have unsigned integers but we're choosing not to because on the whole their disadvantages outweigh their advantages. On the other hand, once we have user-defined value types, you'll be able to define unsigned integers in a library if you want.

The extensibility and power of string templates were a requirement; security experts quite simply vetoed adding string interpolation as it's just too dangerous, especially in server-side software, where Java is mostly used.

As to `STR.` -- which you won't need to use most of the time -- see my other comment: https://news.ycombinator.com/item?id=35013470

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by Kwpolska on Friday March 03, 2023 @03:46PM (#35014780)
Python is also widely used server-side, and they introduced f-strings with simple and friendly syntax a few years ago. JS added template literals in 2015’s ES6, when Node.js was very much a thing. Why is Java special here?
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Friday March 03, 2023 @04:55PM (#35015584)
Java's syntax is just as friendly and simple -- see my other comments on the subject -- it just requires the receiver to define a policy, which is essential for security. You only need to use STR when the receiver does not define a template processor and works with strings. I have no idea what Python's or JS's security experts advised, but that code injection is one of the most common vulnerabilities in memory-safe languages is a fact reported by all security advisories. String interpolation is one of the most dangerous features a language can have.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by Kwpolska on Friday March 03, 2023 @05:27PM (#35015889)
I’m not a fan of the special `log.info."x: \{x}";` syntax, it looks like a weird mix of field access and a string literal.

And even with the fancy new syntax, I’m sure there will be people passing STR."SELECT * FROM x WHERE y = \{y}" to their database. You can educate people all you want, but not all developers will read the docs telling them this is dangerous. Even if all the docs do the right thing, people might end up reading old tutorials, and will then notice the “inefficiency” of the prepared statement syntax and will just do a STR."" format. Or they might consider the database.executeQuery."SELECT \{x}"; syntax “invalid” and try to fix it.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Friday March 03, 2023 @06:41PM (#35016500)
Well, you know, even thirty years of experience as the custodians of one of the most successful programming languages is no guarantee of success with every feature, but that's why we have the preview process now that lets us figure out what problems people actually encounter -- which may well be different from the problems they, or we, think they would encounter -- before finalising the feature.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by avgcorrection on Saturday March 04, 2023 @08:33AM (#35020649)
If I had a cent for every time pron appealed to Java’s age and “popularity” in technical arguments, I would have at least enough cents to use a dollar-sign in string template syntax.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @08:51AM (#35020741)
That's cute, but that appeal is crucial because technical arguments that are divorced from empirical results in the field are a great way to spend a lot of time solving the wrong problems. For example, you can make technical arguments in favour of either VHS or Betamax, but while they both had superior solutions to two separate problems, one of those problems (recording time) ended up being a much more important problem for more people than the other. The purpose of engineering is not to enjoy intellectual puzzles, but to solve problems for people. If you insist that the team that does better than other teams, over and over for a few decades, is doing it all wrong, it's possible that what you consider to be right and wrong doesn't really fit with what's happening in the field.

The relatively new concept of preview features helps us do exactly that: gather information on what problems people actually encounter when working with a new feature as opposed to what problems some speculate they'll run into. The best way to influence Java's direction is not to speculate, but to report problems you've actually encountered to the relevant OpenJDK mailing list (amber-dev in this case). If the use of the \ or $ character actually turns out to be a real problem, we expect to find that out during the preview process (which will begin in JDK 21, out this September) and we'll reconsider, only we'll do it based on reports, not speculation.

Engineering is first and foremost about identifying what bigger problems more people actually need solving. Those who misunderstand that will forever be puzzled by why it is that the VHSes usually win over the Betamaxes and will continue betting on the wrong horses.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by avgcorrection on Saturday March 04, 2023 @11:07AM (#35021818)
Well, I’ll agree that one shouldn’t be naive about the “technically superior” arguments. One has to look at why certain things succeeded. In an honest and non-ideological way (e.g. don’t blind yourself on the “technically superior” talking points, whatever that means).

I don’t really have a problem with a backslash over a dollar-sign. I just couldn’t resist...

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by optician_owl on Saturday March 04, 2023 @02:00AM (#35018852)
I would argue that jdbc API is far more dangerous than string interpolation :)

But I wholeheartedly support the idea of JEP. Scala got several good UX benefits based on the string interpolation (sql and logging).

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by zelphirkalt on Saturday March 04, 2023 @04:54AM (#35019652)
When I want to write a query, I will use a prepared statement or read the docs of a library I am using to interact with a database, whether that library automatically creates a prepared statement. If I use a prepared statement and later read that code, I never again have to ask myself, whether it is safe. Quite frankly, who does not know about injection and prepared statements should not work with databases.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by Kwpolska on Saturday March 04, 2023 @06:03PM (#35025394)
Beginners tend to shoot themselves in the foot fairly often.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by yxhuvud on Saturday March 04, 2023 @08:37AM (#35020679)
Isn't that better solved by not allowing templates to be passed around and blanks filled in later but instead force them to be executed immediately? This is the case in Ruby and while there have been plenty cases of code injection, I've never seen one using the basic string interpolation to do that.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @10:26AM (#35021456)
It's not a better solution but one part of this solution (which also requires templates to originate in the program rather than as input by requiring literals). The rest is done to ensure proper, context-specific quoting/escaping for machine-interpretable constructs, such as JSON and SQL.

As to the dangers of simple string interpolation, here's a warning against using C#'s string interpolation in favour of a safer C# construct: https://bengribaudo.com/blog/2021/04/13/5596/intercepting-st...

We want to make the safe choice the easiest choice, and certainly not the trickier choice. We do that by adding a small tax to the dangerous choice -- i.e. string interpolation. When this feature previews we'll know more about how well this works and can then adjust based on people's actual experience.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by rcoveson on Friday March 03, 2023 @07:13PM (#35016745)
> We could have unsigned integers but we're choosing not to because on the whole their disadvantages outweigh their advantages.

You (collectively) have this wrong; C, C++, C#, Rust, Go, et al. have it right. As long as we're just stating our beliefs outright instead of justifying them, that is. (That's not a request to justify your opinion; I'm sure it's been argued to death already.)

> security experts quite simply vetoed adding string interpolation as it's just too dangerous

Laughable. In an alternate universe where it didn't already exist, this could have been a JEP for StringBuilder and they would have vetoed that too.

> As to `STR.` -- which you won't need to use most of the time

Except for when you're trying to... make a String? Which is what ~everybody who ever asked for string interpolation in Java wanted to do?

What year would you estimate it will be when, at the average Java job, I can expect that most of the library APIs available to me which were often invoked with the form `func(String.format(...))` will have similar APIs that behave as JEP 430 template processors? Maybe after it's been full release for 3 years? 2, optimistically? Because during those years, in order to use this thing at all, people will be using `STR."\{}"`

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Friday March 03, 2023 @08:31PM (#35017226)
It's not a matter of what's right or wrong, but what's appropriate for languages with different audience size. Our experience is at evolving a language for a certain audience size, and perhaps if Java's popularity declines sufficiently to reach the more modest market share of the languages you mentioned, their choices would be appropriate for Java, too.

As to your other points, like I explained to someone else here, even three decades of experience evolving a language that's achieved a measure of success is still no guarantee that every feature will work well, which is why most big new features, string templates included, are first released as preview, which allows us to test our hypotheses against the problems people actually encounter rather than those people speculate they'd encounter, and adjust the feature accordingly before it is finalised. To the best of my knowledge, all preview features have undergone some adjustment before becoming permanent, but usually the changes required were relatively small.

Also, we try to think more long-term, because in ten years no one is going to care or even remember if a feature arrived five or seven years prior.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by kaba0 on Saturday March 04, 2023 @02:25AM (#35018974)
Java still sits very close to JVM byte code, which is typed - adding unsigned ints would duplicate a huge deal of instructions.

So this feature has to wait for primitive classes, where it can be as simple as

  primitive class unsigned {
    int value;
  }
(Though it would need operator overloading to be used as a+b, which is not really liked in Java designer niches (with many good reasons against it) :D )
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by saagarjha on Saturday March 04, 2023 @03:04AM (#35019144)
There is exactly one good reason against operator overloading, and that’s operator<<. People are mostly sane otherwise and it’s a good thing to have.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by kaba0 on Saturday March 04, 2023 @03:19AM (#35019214)
Arbitrary overloading can be problematic, like !#< or whatever is quite hard to look up (scala, haskell), but I agree that implementing an interface/trait that will give you that syntactic sugar is mostly okay (like Rust, e.g. implements Add would give you + operator).
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by vips7L on Saturday March 04, 2023 @07:39AM (#35020377)
I remember working in some scala that used akka actors and seeing ! for the first time. Something like:

  actor ! someData
It was just an operator to send the data to the actor. A named function would have been so much better.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by imtringued on Saturday March 04, 2023 @05:16AM (#35019764)
Just take the operator overloading from Ceylon which has been designed to only allow mathematical use cases and harder to abuse for anything else. When you see an expression a + b it might not be two numbers but it will most likely be associative, distributive and commutative.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by gergo_barany on Saturday March 04, 2023 @08:48AM (#35020730)
A lot of seemingly primitive Java syntax already expands to method calls: autoboxing, string conversions, string concatenation. Similarly, primitive arithmetic operators on unsigned integers could expand to calls to the corresponding Integer.{compare,divide,remainder,...}Unsigned methods. The compiler would turn these back into single machine instructions anyway. This would not need any new bytecode instructions.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by kaba0 on Saturday March 04, 2023 @09:15AM (#35020886)
Sure, though I do think that the currently existing rift between the primitive and object world should be healed first, to not leave behind another construct that will only be a backwards compatibility hindrance later.

(If I’m not mistaken, the existing unsigned functions already compile down to efficient machine code, so in the meanwhile that works)

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by imtringued on Saturday March 04, 2023 @05:11AM (#35019730)
The problem with unsigned is the ridiculous underflow behavior. Unsigned integers are inherently unsafe without underflow checking at runtime. When you are using signed integers for a positive number, then seeing a negative number tells you everything you need to know (the program is wrong and it will most likely crash soon). However, when you are using unsigned to mean a number that can never be negative, then you can't just expect people to never underflow it even just to -1 because it will turn into a large positive number by accident. There is a semantic gap between the meaning of the type and what invariants it guarantees at runtime and that means having no unsigned integers is better for most code unless the unsigned Integer is fully underflow checked at runtime.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by yxhuvud on Saturday March 04, 2023 @08:27AM (#35020610)
> unless the unsigned Integer is fully underflow checked at runtime.

Sure. That doesn't mean the datatype wouldn't be nice to have even with such checks. There are languages out there that fully check over- and underflow. Java could too.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by zarzavat on Saturday March 04, 2023 @10:13AM (#35021327)
I think you are confused. Unsigned arithmetic is modular. Unsigned integers are both positive and negative.

0b1111_1111 is congruent to both -1 and 255 (mod 256). We simply choose the value 255 as being the principle value for coercions, but you could equally well choose -1.

Is 3pm tomorrow or yesterday? Yes.

Unsigned != non-negative.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by VagueMag on Monday March 06, 2023 @03:32PM (#35047166)
My favorite example of this was the major outage at a Swedish futures exchange in 2012 where the system wound up treating an unsigned order quantity as "negative" and placed an order for 4 billion futures contracts: https://www.reuters.com/article/markets-sweden-bug-idUSL5E8M...
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by chlorion on Friday March 03, 2023 @09:47PM (#35017636)
> We could have unsigned integers but we're choosing not to because on the whole their disadvantages outweigh their advantages. On the other hand, once we have user-defined value types, you'll be able to define unsigned integers in a library if you want.

What disadvantages do unsigned ints have?

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by kaba0 on Saturday March 04, 2023 @02:25AM (#35018982)
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @06:39AM (#35020121)
Underflows are much more common than overflows and people again and again fall under the impression that unsigned integers are a good way to represent positive numbers, which is not at all what they do. E.g. C++ is very unhappy with the mistake they made of representing sizes with unsigned types [1].

Unsigned types are also primarily useful when the types have very few bits -- like in bitfields -- but Java doesn't have those, and the uses of unsigned types in Java would be mostly restricted to interaction with native code and hardware, that not many do and for which we have other solutions. So some very dangerous disadvantages and not many advantages for a language like Java.

[1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p14...

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by gergo_barany on Saturday March 04, 2023 @09:26AM (#35020939)
> E.g. C++ is very unhappy with the mistake they made of representing sizes with unsigned types [1].

95% of that unhappiness seems to be due to mixed signed/unsigned expressions with implicit conversions. Java wouldn't be forced to repeat the mistake of making conversions implicit.

The other 5% of the unhappiness is "machine integers are not mathematical integers". But that's the case for signed integers anyway. Overflowing your unsigned integer is almost certainly wrong, but overflowing your signed integer is almost certainly wrong as well.

> not many advantages for a language like Java.

Agreed.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @09:42AM (#35021031)
> 95% of that unhappiness seems to be due to mixed signed/unsigned expressions with implicit conversions. Java wouldn't be forced to repeat the mistake of making conversions implicit.

Yes and no. As Stroustrup's document says, this is an inevitable result of allowing arithmetic on unsigned integers (i.e. subtracting an unsigned int variable with the value 2 from an unsigned variable with the value 1 has the same effect as explicitly mixing it with a signed integer by adding a -2). The only way to avoid that is to add runtime underflow checks to unsigned arithmetic. Once we have user-defined value types, people can choose to do just that, but there seems to be little point in adding it to the core language; the cost/benefit just isn't there.

> overflowing your signed integer is almost certainly wrong as well.

True yet less likely because most integer values appearing in programs are small, and so underflows are more common than overflows.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by cesarb on Saturday March 04, 2023 @11:09AM (#35021830)
> True yet less likely because most integer values appearing in programs are small, and so underflows are more common than overflows.

That's a dangerous argument. The values might be small, until an attacker provides a large one. Your code has to work properly with all possible values, regardless of which ones are more "likely" or "common". In my experience, unsigned makes this simpler, since there are only two cases to consider (zero or positive) instead of four (zero, positive, negative, and INT_MIN); I've expanded on this in a comment here an year ago (https://news.ycombinator.com/item?id=29770689).

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @11:27AM (#35022019)
That's one way of looking at it; another is that the common bugs due to underflow with small numbers themselves create more exploitable vulnerabilities. Anyway, I think your point of view is valid, but it will have to become much closer to a consensus before Java considers changing the status quo and adding unsigned types to the core language.
Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by gergo_barany on Saturday March 04, 2023 @12:51PM (#35022816)
> As Stroustrup's document says, this is an inevitable result of allowing arithmetic on unsigned integers (i.e. subtracting an unsigned int variable with the value 2 from an unsigned variable with the value 1 has the same effect as explicitly mixing it with a signed integer by adding a -2).

This is true for C++. It is a meaningless statement in a language where such mixing is not allowed. The semantics of unsigned modular arithmetic can be defined in its own terms, without dragging signed arithmetic and forced implicit conversions into it.

The semantics of "subtracting" (unsigned, modularly) 2 from 1 is UINT_MAX. This may or may not be what the user wanted. The semantics of subtracting signed 2 from a signed variable called `arrayIndex` with the value 1 is -1. This is almost certainly not what the user wanted. Why should the language police one case and not the other? It doesn't know the context.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @01:54PM (#35023368)
> The semantics of subtracting signed 2 from a signed variable called `arrayIndex` with the value 1 is -1. This is almost certainly not what the user wanted. Why should the language police one case and not the other? It doesn't know the context.

Because in many situations it does know the context or figures it out quickly. Unlike the former case, if you compute a size and get a negative result you can tell it's wrong, and most uses would result in an immediate exception. It's much harder to detect a bad unsigned value. You can think of the sign bit as a bad value bit for values that are supposed to be positive. An alternative would be to add runtime checks that throw an exception on underflow, but that's already a much bigger change in the service of a feature that very few use to begin with.

Those few who wish to work with unsigned types can do so efficiently in Java with the unsigned methods in Integer and Long that were added in JDK 8 (https://docs.oracle.com/en/java/javase/19/docs/api/java.base...). Those relatively rare uses, however, don't merit support in the core language, especially when they're known to bring danger with them. A popular library may choose to return sizes as unsigned values -- like C and C++ did with size_t, something many consider a mistake (although there there are many more valuable uses of unsigned types in C/C++ thanks to bitfields), and now unsuspecting users who otherwise have no need for unsigned types -- i.e. the vast majority of users -- are susceptible to a new kind of common bug. It's just not worth it.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by gergo_barany on Saturday March 04, 2023 @04:09PM (#35024527)
> It's much harder to detect a bad unsigned value. You can think of the sign bit as a bad value bit for values that are supposed to be positive.

You can also think of NaNs and infinities as bad value markers for values that are almost certainly supposed to be finite values. You can also think of the CPU's overflow flag as a bad value marker for signed arithmetic that wrapped around. In these cases Java happily allows me to compute logically incorrect values (I'm aware of Math.addExact etc.). Such bad value bugs happen, and sure they can often lead to exceptions, but not always, and often not immediately so that they are easy to track down. I still don't think unsigned values are somehow different in this regard and must be checked.

> Those few who wish to work with unsigned types can do so efficiently in Java with the unsigned methods in Integer and Long that were added in JDK 8

Those are helpful because they allow one to get things done, but from the point of view of usability they are the worst of both worlds. They are verbose and unchecked. Porting some tricky crypto or math intrinsic from a specification or a reference C implementation becomes unnecessarily hard: The verbose method names obscure the similarity in the code, and you have no help from the type system to check whether you used the unsigned variants in all the right places. I had the joy of fixing such a bug just a week or two ago.

If I'm doing unsigned arithmetic I need to keep track in my head which values to treat as unsigned. Any later comparison on such values should use Integer.compareUnsigned, but the language doesn't help enforce this. This is exactly what type systems are for. Sure I might still compute wrong unsigned values, bugs happen. But at least I would be applying the intended operations. Telling users to go ahead and compute unsigned values but then allowing them to apply logically incorrect operations on them is actually dangerous. Here you're suddenly not so convinced that the language should be strict?

> A popular library may choose to return sizes as unsigned values

Unlikely. If there are no implicit conversions between signed and unsigned, there's not much you could actually do with such values without lots of casts, unlike in C and C++. Users would rightly complain about making the library harder to use for no benefit. I don't see this happening.

In fact, almost the only thing you could easily do with such a size is do arithmetic on it and then pass the result back into the library as an index. If you got things wrong, the library will throw an IndexOutOfBoundsException. This is exactly what you said why it's OK for signed index calculations to become negative, because "most uses would result in an immediate exception". This would be just as easy or hard to debug as an AIOOBE due to signed arithmetic going wrong.

Anyway. I know I won't change your mind. I get that the feature is not important enough to change the language spec. It's just not worth it? OK, sure. But I don't buy the safety argument.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by cesarb on Saturday March 04, 2023 @09:36AM (#35020996)
> and the uses of unsigned types in Java would be mostly restricted to interaction with native code and hardware

And with network protocols and file formats, which also often use unsigned types. As an example, one of the many pain points when implementing JGit was the lack of unsigned types (https://marc.info/?l=git&m=124111702609723&w=2). Another heavy user of unsigned types would be cryptographic algorithms, but I expect these to be mostly implemented in other languages (like C, C++, or Rust), with a sprinkling of assembly or SIMD intrinsics in performance-critical loops, and called through JNI.

Re:JEP 430: String Templates (Preview) Proposed to Target Ja... by pron on Saturday March 04, 2023 @11:30AM (#35022050)
Java does allow you to work with unsigned types efficiently (look at the methods with unsigned in their names in the Integer or Long classes: https://docs.oracle.com/en/java/javase/19/docs/api/java.base...). But those uses are relatively specialised and the vast majority of Java users don't need that. So it's best to keep them as APIs rather than in the core language.