News for hackers. Stuff that matters.
Re:Java 21: What’s New? by jph on Wednesday August 09, 2023 @04:27PM (#37067927)
The matching syntax looks really good to me:

    // Java 21
    if (obj instanceof Point(int x, int y)) {
        System.out.println(x+y);
    }

    // Older Java
    if (obj instanceof Point p) {
        int x = p.x();
        int y = p.y();
        System.out.println(x+y);
    }
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @04:46PM (#37068148)
Its not substantial improvement, but another invariant which need to be learned and adds more brain load.

I hope someone will design some jvm SimpleLang eventually, which will have very few syntactic constructions but cover all/most of use cases.

Re:Java 21: What’s New? by Blackthorn on Wednesday August 09, 2023 @05:41PM (#37068832)
We're programmers. It's our job to know these things. There really aren't that many to learn. It's a much simpler task than various engineering fields.
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @06:10PM (#37069114)
the job is to produce software and make this process reliable and efficient.

> There really aren't that many to learn.

I think there are a lot to learn in java ecosystem.

Re:Java 21: What’s New? by yazaddaruvala on Wednesday August 09, 2023 @06:36PM (#37069316)
The job is to communicate effectively while producing software more reliably and with less time than someone else that might get hired.

There are many of us who think Java has very little to learn - asking us to go slower - while probably good for society in general, is a losing strategy in a Capitalist economy.

You can keep up, or be left behind. Meanwhile, fortunately the “communicate effectively” is 100x more valuable as “produce software more reliably and faster” ie learning all of these new languages and language improvements.

Re:Java 21: What’s New? by appplication on Wednesday August 09, 2023 @05:05PM (#37068401)
I disagree. One of the major critiques of Java is how verbose and full of boilerplate it is. And sure, you can hand wave that as devs being picky, but this is a real reason people avoid picking up new projects in Java. The verbosity serves no purpose, and less code is generally better.

As for there now being multiple ways to write the same thing, this seems like something that could be easily linted into consistency. So you would really only have to learn the less verbose way for any newer Java projects.

Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @05:09PM (#37068446)
> of Java is how verbose and full of boilerplate it is.

yes, new SimpleLang can have lessons learned during last 20 years implemented, as in example from another commenter: https://news.ycombinator.com/item?id=37068380

Currently, for a given task, Java usually have 5 legacy deprecated ways to solve, and two more in newish Java versions, and dev needs to keep all of them in memory to be able to read and understand other's code.

> As for there now being multiple ways to write the same thing, this seems like something that could be easily linted into consistency.

that's if you don't work with any legacy systems and 3p libraries and never switch projects/jobs

Re:Java 21: What’s New? by user432678 on Wednesday August 09, 2023 @05:26PM (#37068671)
Sounds like Kotlin is exactly such SimpleLang.
Re:Java 21: What’s New? by seanmcdirmid on Wednesday August 09, 2023 @05:36PM (#37068772)
Ya, I'm not sure why people would use Java anymore given that the SimpleLang is pretty good.
Re:Java 21: What’s New? by lmm on Wednesday August 09, 2023 @06:16PM (#37069149)
Kotlin is even worse; it's younger than Java but also has 5 legacy ways to solve the problem and two more in the newer version, it's just that the legacy ways have been removed so any examples you find online won't work any more.
Re:Java 21: What’s New? by unscaled on Wednesday August 09, 2023 @10:54PM (#37071380)
Are there any examples for this?

Kotlin sure moves faster than Java, but except for features that were experimental or unstable to begin with (coroutines, -X flags), I can't think of a case where more than 1 deprecated version exists (and the "deprecated" versions are not nearly as crufty as Java).

Re:Java 21: What’s New? by lmm on Wednesday August 09, 2023 @11:07PM (#37071462)
Those "experimental or unstable" features tend to be the advertised selling points of Kotlin. Particularly for async, the previous "experimental" APIs were the only ones available at the time and the recommended way of using Kotlin.
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @03:59AM (#37073302)
Kotlin is anything but simple
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @03:59AM (#37073300)
You way over exaggerate the number of different ways a given thing can practically be done in Java. Pattern matching for example only has the visitor pattern as a counterpart and you ain’t going around spewing visitor patterns, it not as simple.

Sure there are a few warts, e.g. arrays work a bit differently than Lists, and primitive-object discrepancy, but every language will pick up similar issues over time.

Re:Java 21: What’s New? by piwi on Wednesday August 09, 2023 @05:19PM (#37068573)
My comparison with c++ shows java is not more verbose if you take into account headers.
Re:Java 21: What’s New? by rf15 on Wednesday August 09, 2023 @05:51PM (#37068929)
I think people are very aware that c++ is a lot more verbose than Java. What's the advantage of making this particular comparison?
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @06:07PM (#37069082)
I actually think c++ is not necessary more verbose than java as a language.

But memory management/safety, weaker IDE support and ecosystem make it less productive than Java for business dev.

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:01AM (#37073317)
Wait until you see this new, hyped language called Go! That is even more verbose than both!
Re:Java 21: What’s New? by jimmaswell on Wednesday August 09, 2023 @06:21PM (#37069183)
I actually appreciate the simplicity of bare Java. It can be verbose on one hand but on the other there are no surprises because everything is just a method call on an object with no operator overloading etc. Seems like they're kind of ruining that with bizarre things like "obj instanceof Point(int x, int y))". That doesn't connotate at all that it's calling x() on the point to get x, it looks like a field. Compare with C# which has a ton of convenience features and syntactic sugar but it's all intuitive and self-explanatory. Really I wish Java would go away and .NET would take over but it's too entrenched.
Re:Java 21: What’s New? by the_af on Wednesday August 09, 2023 @09:20PM (#37070684)
Point is a record type here. Record types seem to be like Scala's case classes.

The distinction between fields and getters is intentionally blurred because these are immutable value classes.

Re:Java 21: What’s New? by cutler on Wednesday August 09, 2023 @07:36PM (#37069862)
Yes. Any language which requires an IDE to append get, set and toString for every damned variable is a language in trouble. Here we are at Java 21 and Java must be the only language in modern use which requires regex metacharacters to be escaped. This even after the introduction of raw string literals.
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:01AM (#37073312)
You missed out on Java’s records that solve your gripe with get/set.
Re:Java 21: What’s New? by cutler on Thursday August 10, 2023 @06:31AM (#37074278)
Records are not the same as classes.
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @07:33AM (#37074771)
They are not, but if you need strictly a class for strictly storing data, records are superior in every way. Unfortunately some tools still want POJOs, but most (eg. Converting to json) do work with records.
Re:Java 21: What’s New? by nogridbag on Thursday August 10, 2023 @09:38AM (#37076025)
Disagree with that, but please provide a counterpoint. I've using Java 17 for a large project and I was really excited to use records. I have not found any practical use case for them outside of one or two instances in a project with 2 years of development.

Storing data is also where I thought records would be a good use case. But in the real world data is not Point(x,y). If you have more than 3 or 4 fields in your record, that's simply not going to work. I thought the rest of the industry decided over a decade ago that using positional properties (e.g. new MyRecord(a,b,c,d,e,f,g)) to initialize an object was a bad idea. I think Rich Hickey gave a whole talk on that subject. If there's a future plan to be able to create records using named props, e.g. new MyRecord([firstName: "Bob"]), then I would reconsider my opinion.

Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @10:08AM (#37076357)
Re:Java 21: What’s New? by nogridbag on Thursday August 10, 2023 @06:38PM (#37082627)
Thanks for sharing. It looks interesting.
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.

Re:Java 21: What’s New? by nkh on Wednesday August 09, 2023 @04:52PM (#37068227)
Isn't that Clojure?

(let [p obj]

  (if (instance? Point p)

    (let [x (.x p)

          y (.y p)]

      (println (+ x y))))
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @04:55PM (#37068268)
I think Clojure is not statically typed? which is a big disadvantage, and also syntax is very different from current mainstream languages, so even more effort to learn is required.
Re:Java 21: What’s New? by nkh on Wednesday August 09, 2023 @05:02PM (#37068367)
When type checking is needed, I find the Truss library* does the trick quite well.

As for the syntax, there is very little, which can make it a harder lift but once you have the hang of it you won't deal with the issues identified in the parent comment.

* https://github.com/taoensso/truss

Re:Java 21: What’s New? by The_Colonel on Wednesday August 09, 2023 @05:41PM (#37068829)
I don't know what Truss is, but I know that I inherited a few smaller Clojure projects / services, and it's untyped hell. Having optional typing means most people are not going to use it.
Re:Java 21: What’s New? by cutler on Wednesday August 09, 2023 @07:42PM (#37069903)
JS and Python are not statically typed, or PHP for that matter, but it hasn't stopped JS and Python topping the popularity list year on year nor did PHP stop Facebook from world domination.
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @08:10PM (#37070122)
I think in all cases it was realized that static types are adding significant benefits, so they were added to python, typescript and hack have been developed.
Re:Java 21: What’s New? by cutler on Wednesday August 09, 2023 @08:19PM (#37070193)
JS and Python have gradual, not static typing. Same for PHP 8 but Hack is a different language.
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @08:26PM (#37070248)
> JS and Python have gradual, not static typing.

that's because of all legacy dynamicaly typed codebased

Re:Java 21: What’s New? by kazinator on Wednesday August 09, 2023 @09:57PM (#37070992)
For values of "legacy" referring to "new code written this afternoon, and tomorrow morning".
Re:Java 21: What’s New? by Per_Bothner on Wednesday August 09, 2023 @06:02PM (#37069035)
Or Kawa:

    (if (? p::Point obj)
      (... p:x p:y))
This succeeds if obj matches the pattern p::Point, declaring p to be the value of obj coerced to a Point. This is integrated with Kawa's static type-checking/inference.

https://www.gnu.org/software/kawa/Conditionals.html

Re:Java 21: What’s New? by cutler on Wednesday August 09, 2023 @07:39PM (#37069877)
Clojure to the rescue ... again. Honestly, I can't fathom why Clojure isn't up there with the most popular languages. Paul Graham was right when he described it, well Lisp, as a secret weapon.
Re:Java 21: What’s New? by chii on Wednesday August 09, 2023 @09:15PM (#37070634)
i would imagine the idiomatic clojure to do this is to convert the `p` into a bean using `clojure.core/bean`

but if it is a java record class, then there's currently no standard library way, since it's not a bean (getter/setter naming of properties). You'd have to write custom code for it, which sucks.

Re:Java 21: What’s New? by kazinator on Wednesday August 09, 2023 @09:54PM (#37070960)
TXR Lisp:

Using quasiquote-style pattern:

   (if-match ^#S(point x ,x y ,y) obj
     (prinl (+ x y))
Or using @(struct ...) pattern operator:

   (if-match @(struct point x @x y @y) obj
     (prinl (+ x y))
All done with macros. Expansion, compilation, disassembly:

  4> (flow '(if-match @(struct point x @x y @y) (new (point 1 2))
              (prinl (+ x y)))
       expand
       prinl
       compile-toplevel
       disassemble)
  (let ((#:g0062 (struct-from-args 'point 1 2)))
    (let* (#:result-0065
           x y)
      (if (if (subtypep (typeof #:g0062)
                        'point)
            (let ((#:x0063 (slot #:g0062 'x))
                  (#:y0064 (slot #:g0062 'y)))
              (sys:setq x #:x0063)
              (sys:setq y #:y0064)
              (sys:setq #:result-0065
                (prinl (+ x y)))
              t))
        #:result-0065
        ())))
  data:
      0: point
      1: 1
      2: 2
      3: x
      4: y
  syms:
      0: struct-from-args
      1: subtypep
      2: typeof
      3: slot
      4: prinl
      5: sys:b+
  code:
      0: 2003000E gcall t14 0 d0 d1 d2
      1: 04000000
      2: 04020401
      3: 20010005 gcall t5 2 t14
      4: 000E0002
      5: 20020002 gcall t2 1 t5 d0
      6: 00050001
      7: 00000400
      8: 38000016 if t2 22
      9: 00000002
     10: 2002000A gcall t10 3 t14 d3
     11: 000E0003
     12: 00000403
     13: 20020009 gcall t9 3 t14 d4
     14: 000E0003
     15: 00000404
     16: 20020006 gcall t6 5 t10 t9
     17: 000A0005
     18: 00000009
     19: 2001000D gcall t13 4 t6
     20: 00060004
     21: 1000000D end t13
     22: 10000000 end nil
  instruction count:
     10
  #<sys:vm-desc: 9966330>
I see this is doing something silly: (subtypep (typeof x) y) is just (typep x y), but costs an extra gcall instruction.

Either the pattern matcher should do this, or the compiler should have that as an algebraic reduction, or both.

Let's do the compiler for fun:

  $ git diff stdlib
  diff --git a/stdlib/compiler.tl b/stdlib/compiler.tl
  index 58685741..a837571e 100644
  --- a/stdlib/compiler.tl
  +++ b/stdlib/compiler.tl
  @@ -1411,6 +1411,8 @@ (defmeth compiler comp-fun-form (me oreg env form)
              (set form (rlcp ^(,bin ,a ,b) form)))
             ((- @a)
              (set form (rlcp ^(neg ,a) form)))
  +          ((subtypep (typeof @a) @b)
  +           (set form (rlcp ^(typep ,a ,b) form)))
             ((@(or ignore nilf) . @args)
              (if (eql sym 'ignore)
                (each ((a args))
Note that we are using pattern matching here to recognize and rewrite this case. In return we will obtain better code from pattern matching:

  $ make
  TXR stdlib/compiler.tl -> stdlib/compiler.tlo
  $ ./txr
  This is the TXR Lisp interactive listener of TXR 291.
  Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
  TXR is not a toy, but should be kept within easy reach of children.
  1> (flow '(if-match @(struct point x @x y @y) (new (point 1 2))
              (prinl (+ x y)))
       expand
       prinl
       compile-toplevel
       disassemble)
  (let ((#:g0024 (struct-from-args 'point 1 2)))
    (let* (#:result-0028
           x y)
      (if (if (subtypep (typeof #:g0024)  ;; this hasn't changed, of course
                        'point)
          (let ((#:x0026 (slot #:g0024 'x))
                (#:y0027 (slot #:g0024 'y)))
            (sys:setq x #:x0026)
            (sys:setq y #:y0027)
            (sys:setq #:result-0028
              (prinl (+ x y)))
              t))
        #:result-0028
        ())))
  ** expr-1:1: warning: if-match: no such struct type: point
  ** expr-1:1: warning: new: point does not name a struct type
  data:
      0: point
      1: 1
      2: 2
      3: x
      4: y
  syms:
      0: struct-from-args
      1: typep              ;;; no mention of subtypep here any more!
      2: slot
      3: prinl
      4: sys:b+
  code:
      0: 2003000E gcall t14 0 d0 d1 d2
      1: 04000000
      2: 04020401
      3: 20020002 gcall t2 1 t14 d0
      4: 000E0001
      5: 00000400
      6: 38000014 if t2 20
      7: 00000002
      8: 2002000A gcall t10 2 t14 d3
      9: 000E0002
     10: 00000403
     11: 20020009 gcall t9 2 t14 d4
     12: 000E0002
     13: 00000404
     14: 20020006 gcall t6 4 t10 t9
     15: 000A0004
     16: 00000009
     17: 2001000D gcall t13 3 t6
     18: 00060003
     19: 1000000D end t13
     20: 10000000 end nil
  instruction count:
      9                              ;; down by one
  #<sys:vm-desc: 92c94d0>
Ship it!
Re:Java 21: What’s New? by earthboundkid on Wednesday August 09, 2023 @05:12PM (#37068484)
Someone (I think pg?) said that patterns are just insufficient language features, but the flip side applies too: you can apply a pattern in any language, but a feature is limited to what the language designers provide. Patterns you can learn once and write anywhere (for better or worse).
Re:Java 21: What’s New? by Jtsummers on Wednesday August 09, 2023 @05:22PM (#37068606)
Peter Norvig: http://norvig.com/design-patterns/ (slide 4)
Re:Java 21: What’s New? by earthboundkid on Thursday August 10, 2023 @12:43AM (#37072056)
PN, so I was half right. :-P
Re:Java 21: What’s New? by maxfurman on Wednesday August 09, 2023 @05:20PM (#37068581)
Sounds like Kotlin to me
Re:Java 21: What’s New? by riku_iki on Wednesday August 09, 2023 @05:24PM (#37068638)
in my non-expert opinion, kotlin also added bunch of creative, unobvious and opinionated constructs to not be qualified as "simple lang"
Re:Java 21: What’s New? by cutler on Wednesday August 09, 2023 @07:49PM (#37069965)
Their implementation of closures is ass-backwards. Just a bare pair of braces with no arrow to disambiguate from block parens. Kotlin also added to Java's verbosity with extra modifiers like 'internal', 'open' and 'by lazy' which isn't even grammatical English. And don't get me started on multiple constructors.
Re:Java 21: What’s New? by eastbound on Wednesday August 09, 2023 @05:03PM (#37068380)
Oh yes, without the .stream()….collect(toList()) cruft! Just lists with .map() on them!

Also without the :: operator, it’s uglier than the arrow equivalent and it’s yet-another-construct to parse.

Re:Java 21: What’s New? by sbjs on Wednesday August 09, 2023 @06:06PM (#37069067)
That was literally the point of Java, to be syntactically and semantically simple, in contrast to the extreme complexity of C++. But just like with Lisp, that only moves necessary complexity to another layer. True wisdom in language design is understanding the line between necessary and unnecessary complexity, and perfectly incorporating necessary complexity into the language so users don't have to reinvent the wheel at the library or precompiler level.
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @03:55AM (#37073263)
Java is quite a lean language, especially considering its age, and the design team does consider every new feature for a long time before adding, being an exception among languages with regards to feature churn (with all its negatives and positives).

Just look at C# or C++, both have at least an order of magnitude larger language “surface area”.

Re:Java 21: What’s New? by nxobject on Thursday August 10, 2023 @04:58AM (#37073670)
Beyond the other posts, I wonder whether some of the reactions are due to the fact that destructuring here is purely syntactic sugar – there's no "fear of missing out" on new semantics that offer a drastic new improvement in programming style, to the extent that they feel obligatory to incorporate in your mental model of Java (e.g the introduction of anonymous functions.)
Re:Java 21: What’s New? by trondeh80 on Thursday August 10, 2023 @04:34PM (#37081397)
Kotlin is pretty close to this?
Re:Java 21: What’s New? by riku_iki on Thursday August 10, 2023 @04:38PM (#37081449)
this was discussed here in comment several times, and opposite opinion is that kotlin added enough creatives and opinionated constructs which make it more innovative and less simple.

I think the closest thing is if someone would create linter for java which would allow only essential structures and library API. Java has very good foundational core.

Re:Java 21: What’s New? by brap on Wednesday August 09, 2023 @04:47PM (#37068159)
Thanks I hate it
Re:Java 21: What’s New? by the_af on Wednesday August 09, 2023 @05:33PM (#37068745)
You hate that it takes 2 lines instead of 4 (excluding curly braces) to accomplish the same? The new syntax is not even convoluted, it's pretty intuitive destructuring syntax already available in many modern languages.
Re:Java 21: What’s New? by morepork on Wednesday August 09, 2023 @07:20PM (#37069716)
There's potential confusion whether the int x from the example comes from point.x (field), point.x() (method) or point.getX() (common getter pattern), just one more gotcha
Re:Java 21: What’s New? by the_af on Wednesday August 09, 2023 @08:20PM (#37070208)
By reading the proposal [1], I see it's meant for record types. Records seem to be value classes, i.e. the equivalent to Scala's case classes.

In that case, there's absolutely no ambiguity: there's only one `x` you can be talking about.

By the way, value/case classes are such a boon. Very easy to get used to, and once you have, you want to use them everywhere, I guarantee.

----

[1] https://openjdk.org/jeps/440

Re:Java 21: What’s New? by morepork on Thursday August 10, 2023 @01:38AM (#37072371)
Thanks that makes more sense, I had assumed it was for all types as Point used in the example has the same name as a class in the standard library
Re:Java 21: What’s New? by trealira on Wednesday August 09, 2023 @05:13PM (#37068493)
And that "older Java" example itself is an alternative (added in Java 16, from looking it up) to what existed before.

  if (obj instanceof Point) {
      Point p = (Point) obj;
      int x = p.x();
      int y = p.y();
      System.out.println(x + y);
  }
Re:Java 21: What’s New? by monksy on Wednesday August 09, 2023 @05:26PM (#37068660)
Scala

obj match { case Point(x,y)=> println(x+y) }

Re:Java 21: What’s New? by diarrhea on Wednesday August 09, 2023 @05:50PM (#37068921)
Very similar in Python and Rust as well.
Re:Java 21: What’s New? by switchbak on Wednesday August 09, 2023 @05:52PM (#37068952)
Sadly no one wants to talk about Scala any more.
Re:Java 21: What’s New? by throwaway5959 on Wednesday August 09, 2023 @05:57PM (#37068996)
Even for Spark?
Re:Java 21: What’s New? by monksy on Wednesday August 09, 2023 @07:45PM (#37069929)
We don't talk about spark
Re:Java 21: What’s New? by throwaway5959 on Thursday August 10, 2023 @02:30PM (#37079916)
What? Why?
Re:Java 21: What’s New? by klysm on Wednesday August 09, 2023 @05:59PM (#37069005)
Pretty much the same as C#
Re:Java 21: What’s New? by coding123 on Wednesday August 09, 2023 @06:24PM (#37069201)
What is it saying here. Is the (int x, int y) from getters or the constructor? It LOOKs like a constructor, but I can't possibly imagine it's doing anything with the constructor in this call....

Which probably explains the person saying "I hate it".

Re:Java 21: What’s New? by valenterry on Thursday August 10, 2023 @02:53AM (#37072831)
No, I think this only works on Java Records where it's clear what the identifiers are.
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:18AM (#37073431)
You are destructuring the object here, and since it is a record type that is nothing more than its typed, ordered fields listed in the constructor, you just use it as its reverse function here, basically.

Destructuring for other kind of objects are planned with user-defined destructors. But it does have plenty prior art, e.g. in haskell you can deconstruct lists arbitrarily like _:secondElem:rest would bind secondElem logically.

Re:Java 21: What’s New? by morepork on Wednesday August 09, 2023 @07:16PM (#37069694)
It's a bit of an unkind example as most would write the latter as

    if (obj instanceof Point p) {
        System.out.println(p.x()+p.y());
    }
But I can maybe see the value if you need to use x/y more than once.

However in Java it's common to have getX() style accessors, for which this syntax doesn't seem that helpful as your variable would end up being called getX

Re:Java 21: What’s New? by valenterry on Thursday August 10, 2023 @02:52AM (#37072826)
I think this syntax for accessing x and y only works for java records anyways and not for methods.