News for hackers. Stuff that matters.
Re:Java 21: What’s New? by pizza234 on Wednesday August 09, 2023 @05:24PM (#37068630)
Destructuring (/pattern matching) itself is supported by several languages (although more in general, its flexibility depends on the language). It makes sense to support it in modern languages, as it's practical and intuitive.

IMO it's a significant syntactical improvement, as it considerably reduces boilerplate assignments.

Re:Java 21: What’s New? by The_Colonel on Wednesday August 09, 2023 @05:35PM (#37068769)
Destructuring is nice, but this here seems to work only in conjunction with instanceof which is a quite rare (and somewhat smelly) operator.

I mean, how are you going to use it? Something like this?

    Point p = getPoint();

    if (p instanceof Point(int x, int y)) {
        System.out.println(x+y);
    }
... that seems pretty bad. Or am I missing something?
Re:Java 21: What’s New? by the-smug-one on Wednesday August 09, 2023 @06:16PM (#37069151)
You'd do if (getPoint() instanceof ...) if you know it's a Point. But then, you probably don't need to deconstruct it anyway. You can just do p.x+p.y.

This type of pattern matching is mostly useful when you have some T and a few cases, there is also a switch/case equivalent for when you've got a lot of them.

Maybe we'll see some syntax for deconstructing values directly into variables, a la Python in the future.

Re:Java 21: What’s New? by heavyset_go on Wednesday August 09, 2023 @06:19PM (#37069163)
You can have a parent class/interface with many subclasses/implementations.

You can then use pattern matching to switch on instances of those subclasses while destructuring objects to access attributes at the same time. Kind of like enums and pattern matching in Rust.

In your example there is no reason to use `instanceof` with `p`, you already know what it is.

Re:Java 21: What’s New? by The_Colonel on Thursday August 10, 2023 @01:50AM (#37072429)
> You can have a parent class/interface with many subclasses/implementations.

Sure, but in Java the standard handling of that is polymorphism. instanceof has been so far a bit of an antipattern, breaking out of the OOP model.

> In your example there is no reason to use `instanceof` with `p`, you already know what it is.

That's my point. This seems like a very limited version of destructuring if you can use it only when you don't know the type. Like in JavaScript you'd routinely write something like:

    const {x, y} = getPoint();
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:07AM (#37073364)
There is subtyping.

A more practical (though not necessarily realistic) example might be:

  sealed interface List<T> permits Cons<T>, None<T> {
    record Cons<T>(T head, List<T> tail) {}
    record None<T>() {}
  }


  // some function only wanting the  first elem of a list
  switch (List<T> list) {
    case Cons<>(T head, var tail) -> // do something
    default -> {}
  }
If you have used Haskell or similarly strongly typed FP language, you can see how great pattern matching can be.
Re:Java 21: What’s New? by heavyset_go on Thursday August 10, 2023 @04:52PM (#37081631)
> Sure, but in Java the standard handling of that is polymorphism. instanceof has been so far a bit of an antipattern, breaking out of the OOP model.

I think that's deliberate, Java is moving towards the modern programming zeitgeist that doesn't really care about strict adherence to the OOP model. This is a feature that comes from languages that don't adhere to Java's OOP principles.

I didn't downvote you, by the way. Don't know why that happened.

Re:Java 21: What’s New? by hibikir on Wednesday August 09, 2023 @06:23PM (#37069198)
The idea is that you deconstruct when a method returns a sum type: An interface with a defined set of implementers that doesn't change. A very common pattern in, say, Haskell or Scala. In Scala you'd also get a warning if you forgot to check one possible type to deconstruct. If you aren't defining your data types like that, yeas, it's a bit weird.

This, along with the underscore, and even the sequence type, shows that the language designers are well aware of what Scala does, and are picking and choosing which things to bring in without risking exposing Java devs to the traditional monad salads that you see in many a Scala codebase.

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:10AM (#37073384)
You also get compile-time error in Java switch expressions if you are not exhaustive in your cases.

But yeah, of course the design team knows about features from Scala, they are probably one of the most competent people on the Earth when it comes to PLs, and are literal CS giants.

Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @05:26PM (#37068659)
> It makes sense to support it in modern languages

the problem is that Java not modern language, but carrying all legacy features from last 20 years, so having all legacy features + new modern creative ideas make Java very complex.

Re:Java 21: What’s New? by unlikelytomato on Wednesday August 09, 2023 @06:51PM (#37069452)
What are some examples of these legacy features?

Overwhelmingly, I see complaints that java has been too simple compared to virtually all other languages. I agree that cognitive load is important. However, it feels really strange to me to suggest that java has reached the complexity of other similar languages. What am I missing?

Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @07:04PM (#37069575)
for example Java went through several generations of IO evolution, so some simple task like reading text file line by line can be done in many different way in current standard library:

- using DataInputStream

- using Scanner

- using Reader

- using Files.readLines

and they all suck.

Re:Java 21: What’s New? by unlikelytomato on Wednesday August 09, 2023 @07:59PM (#37070045)
I see. Is it fair to say that most of the issues area related to jdk included libraries vs language features itself? I also find a lot of the included libraries to be cumbersome, but I see these syntax improvements to be a welcome change in contrast.
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @08:07PM (#37070100)
> Is it fair to say that most of the issues area related to jdk included libraries vs language features itself?

I think they all connected, for example with added lambdas they also added streams, so collections library now allows to do the same thing using old iterators or new streams APIs, and somehow those APIs are not compatible, it is not easy to produce stream from iterator.

Re:Java 21: What’s New? by erik_seaberg on Thursday August 10, 2023 @12:39PM (#37078393)
Iterator is a very simple API that lots of third party code implements, but streams rely on spliterators for concurrency. You can sort of get a spliterator from any iterable and make a stream, but it will probably do all work sequentially.
Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @12:43PM (#37078468)
> but streams rely on spliterators for concurrency

which I never seen anyone uses and it makes everything more complicated

they probably should've have some ParallelStream interface

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:03AM (#37073331)
That’s not language, but ecosystem. And I much prefer this to randomly breaking previously working code.
Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @05:34AM (#37073907)
its standard library.

core ecosystem, e.g. apache commons will add several more ways.

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @07:35AM (#37074800)
IO has so many usecases that frankly, it makes sense. In a way a language with only one way of doing it is missing out on some another use case.
Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @12:08PM (#37077982)
python and c++ both have the same API for 20-30 years I think, and they are both more robust than Java's APIs imo for 95% of cases.
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @12:14PM (#37078067)
Agree to disagree on that one.

Java’s standard library is one of the best stdlibs out there.

Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @03:52PM (#37080900)
Evidence of opposite in that example:

programmers in 95% cases want something like following to read lines from text file:

for s in open('file.txt'): print(s)

Java has 5 different io APIs and no one provides such construct, you need to stack various verbose abstractions to just read text file.

How it is the best? Ergonomics and brain load is very bad.

Re:Java 21: What’s New? by Gibbon1 on Thursday August 10, 2023 @03:59PM (#37080978)
I'm mostly write embedded code. But when I have to deal with files and associated API's I'm always like why does anyone tolerate this stuff.

  string[] lines = ReadFile("path", "filename");
Why is the above somehow hard?
Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @04:14PM (#37081162)
actually your code is not well designed too:

- it will produce OOM if file is very large

- iterating array is usually verbose in many of languages (at least in Java)

- not sure why you would want to have path and filename as separate params

In Java, it would be more ergonomical if they add something like following:

Iterable<String> Files.readAllLines(String fileName);

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:39PM (#37081470)
How is OOM not a concern in case of C++, python? Java’s List is much more ergonomic that arrays.

Also, I really don’t understand your last example - that’s literally available in java returning a List of lines. If you add a .stream().collect(joining(“\n”)), you get the whole string in one go.

Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @04:43PM (#37081516)
> How is OOM not a concern in case of C++, python?

I think in python it will lazely iterate through the file, and not load everything at once

> Java’s List is much more ergonomic that arrays.

right, but commenter above proposed array..

> that’s literally available in java returning a List of lines.

current API has two issues:

- OOM if file is large

- it takes Path instance which you need to construct and not "String file" parameter, thus hit on ergonomics

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @05:12PM (#37081870)
Path.of(yourString)
Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @05:18PM (#37081919)
I know, but why it is necessary?
Re:Java 21: What’s New? by Gibbon1 on Friday August 11, 2023 @03:52PM (#37093187)

  - it will produce OOM if file is very large
A well designed API won't give you an OOM it'll throw a 'this file is too big'

  - not sure why you would want to have path and filename as separate params
Throws path not found. Throws file not found. Why does the programmer need to deal with twee-fiddly path magic for say each file in a directory of config files?

  Iterable<String> Files.readAllLines(String fileName); 
Oh sure that's totally more readable.

  I think in python it will lazely iterate through the file,
I wrote a program to suck in a list of log files and generate a bunch of metrics and display them. Users liked to select a dozen 100MB log files. Lazy iteration was way way too slow. Fastest was suck each one into memory and call split.
Re:Java 21: What’s New? by riku_iki on Friday August 11, 2023 @04:28PM (#37093617)
> A well designed API won't give you an OOM it'll throw a 'this file is too big'

this is possible solution, still it doesn't allow to iterate lines one by one and handle large files

> Throws path not found. Throws file not found.

I think it is some very niche use cases when you need to distinguish between these cases. From another hand there are many places in the code which operate absolute file paths already, and you force them to split path, which is cumbersome. At least two alternative APIs probably are justified to have.

> Fastest was suck each one into memory and call split.

I really don't think pre-splitting is any kind of performance bottleneck, it is likely 1% of performance compared to all IO stack and subsequent string/objects memory management.