Hacker News Re-Imagined

Java record pattern matching in JDK 19

  • 208 points
  • 8 days ago

  • @SemanticStrengh
  • Created a post

Java record pattern matching in JDK 19


@zelphirkalt 8 days

Replying to @SemanticStrengh 🎙

Another feature, that Java finally adopts, after decades of not adopting it. More and more language features and concepts are being introduced, finally exposing Java-only programmers to them. Lambdas, pattern matching, project Loom, at some point in the future, Java might be called a modern language. Good for Java and Java programmers.

Just checked some other languages:

- SRFI (Scheme Request for Implementation): https://srfi.schemers.org/srfi-200/srfi-200.html

Syntax:

> | ($ record-name pat_1 ... pat_n) a record

- GNU Guile: https://www.gnu.org/software/guile/manual/html_node/Pattern-...

Seems to be implementing the SRFI:

> | ($ record-name pat_1 ... pat_n) a record

- Rust: https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html and https://doc.rust-lang.org/reference/patterns.html#struct-pat...

> We can also use patterns to destructure structs, enums, and tuples to use different parts of these values. Let’s walk through each value.

> Struct patterns match struct values that match all criteria defined by its subpatterns. They are also used to destructure a struct.

Reply


@peterkelly 8 days

A great paper on this topic:

D. J. Pearce, “Sound and Complete Flow Typing with Unions, Intersections and Negations,” in Verification, Model Checking, and Abstract Interpretation, vol. 7737, R. Giacobazzi, J. Berdine, and I. Mastroeni, Eds. Berlin, Heidelberg: Springer Berlin Heidelberg, 2013, pp. 335–354. doi: 10.1007/978-3-642-35873-9_21.

https://ecs.wgtn.ac.nz/foswiki/pub/Main/TechnicalReportSerie...

Reply


@mabbo 8 days

It's not the feature that excites me, it's the pace of Java language improvements. The 6-month cycle of versions, the whole process of letting experimental features in for a couple versions to test, refine, and likely accept, it's all really working, you know? We're getting stuff that's been so long desired, and it's happening quickly enough to be exciting but not so fast that we're overwhelmed and things are breaking.

People often say that Java 8 was the big important update to the language (streams, lambdas, java.time, and more), but I think Java 9, which began this process was the true rebirth of the language.

Reply


@cutler 8 days

Despite recent improvements including raw string literals it beggars belief how Java still requires regex metacharacters to be escaped. Until that's fixed Java is not an option for me.

Reply


@naruhodo 8 days

Great! Java is getting destructuring.

    static void printUpperLeftColoredPoint(Rectangle r) {
        if (r instanceof Rectangle(ColoredPoint ul, ColoredPoint lr)) {
             System.out.println(ul.c());
        }
    }

But lr is not used in this example. Where is the syntax for ignoring a component? Can we drop lr, or use a placeholder like '_' multiple times?

Reply


@ackfoobar 6 days

Coming from Scala, I find positional decomposition a huge mistake.

In Scala, constructing a `case class` allows positional/named arguments, which prevents some bugs. But destructring has to be positional. Where is the mathematical beauty of duality?

Also if I only want a few fields from a wide record, I have to add the underscores for all other fields. `case SomeClass(_, _, _, field1, field2) =>` And when I add another field, all those `case`s have to be changed.

OCaml gets it right. Pattern matching records uses named fields. It has field punning, you only have to write the name once if new variable's name is the field name (like JS). Extra fields are discarded with one single `; _` in the pattern.

I don't really care what Java does. I just hope Kotlin won't mess it up.

Reply


Cool to see Java getting these features. After having been a Java dev most of my life, and then moving to Typescript years ago, it's hard to believe I went so long without this.

Related, Typescript is the only language I know of that through flow analysis does not require you to redefine the variable. E.g. you can do something like

  if (typeof foo === 'string') {
    //Typescript knows foo is a string here
  }
All the other languages I know of, like this proposal for Java, require the variable to be redefined. I personally find the Typescript way, as VSCode will give you appropriate type warnings in the right places, easier to use in practice. Just curious if there are any other languages that do it like TS.

Reply


I love seeing modern language features coming to Java! Sadly, I suspect it will be at least a decade before I see Java 19 used anywhere I work...

Reply


I am an old school java developer. Can someone explain me why this is a "feature"? I can remember by counting my fingers the times I had to use "instanceof", and it was some classical reflection/injection/instantiation framework 'vodu'. If you are using instanceof in a normal java program, you are just making it wrong. It looks like some new javascript guys arrived in the block and are trying to make java look something messy like Scala. What real world problem is this trying to solve? Are you just fucking bored because you are all out of ideas and every JDK release we need "something"? Why these JEPs only have "boxes,rectangles,A,B,I" class examples and not a simple real world class scenario? Why we need to make java "friendly" to work with generic objects? it should be hell! I cant wait for JDK 50.

Reply


@jsiaajdsdaa 8 days

I really feel Java already has everything I need to do my work. More improvements are always welcome of course.

Reply


For everyone saying the visitor pattern requieres switch statements this is false. The visitor can build its case inside the iterface/implemetation. If the complexity is being solved by the "housing" class that the visitor will be visiting you are not doing enough.

Reply


About Us

site design / logo © 2022 Box Piper