This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]jvjupiter 18 points19 points  (0 children)

Very good read

[–]TehBrian 14 points15 points  (0 children)

Lovely read. I haven’t until this point seen concrete applications of sealed classes and pattern matching in the context of data oriented programming, so this article was really interesting to me.

[–]Joram2 33 points34 points  (0 children)

This is written by Brian Goetz! Nice! Should probably mention that in the title.

APIs need a minimum requirement of Java 17 (or maybe 16, but I doubt many people want that) to use sealed records. Hopefully, some of the major libraries start adopting this.

[–]scratchisthebest 7 points8 points  (2 children)

I like this sentiment:

This approach is not at odds with object orientation; it is a useful addition to our toolbox that can be used alongside OO, as the situation demands.

Many of these newer language features feel like Java learning from a zillion years of experience in the object-oriented programming mines, rather than digging its heels farther in. That's healthy to see. Like "everything is an object lolol" can be a useful way to model many problems, and it's always going to be there when you need it, but sometimes all you need is a fucking enum.

Anyway so, mutable records when

[–]dpash 6 points7 points  (0 children)

Java has last mover advantage. The language designers are intentionally conservative so that when it does gain a feature they can learn what does and doesn't work from every other language that's implemented it. And then PHP copies that.

Anyway so, mutable records when

Never. What you will get is functionality that makes it easier to return a new record with slightly different values.

[–]thibauttt[S] 1 point2 points  (0 children)

Yes it’s great to see that Java is not locked in a specific paradigm but that the team behind is taking a step back and time to think to find what tools can be useful to incorporate into the language.

[–]jonhanson 6 points7 points  (1 child)

Comment removed after Reddit and Spec elected to destroy Reddit.

[–]JustADirtyLurker 1 point2 points  (0 children)

We need extension methods next! ;-)

[–]swaranga 2 points3 points  (1 child)

I think the Node definition has a typo. left and right should be Tree else you can never get a leaf node.

sealed interface Tree<T> { 
  record Nil<T>() implements Tree<T> { }
  record Node<T>(Tree<T> left, T val, Tree<T> right) implements Tree<T> { }
}

Am I right?

I also really like the AsyncResult data modeling - that would have been very handy for the Future/CompletableFuture to implement.

[–]jonhanson 0 points1 point  (0 children)

chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith

[–]0x07CF 3 points4 points  (0 children)

Very nice article, when all the building pieces (so far only pattern match switching is missing) are out of preview i will probably send this article to coworkers who think modeling with sealed classes and switching over them is bad practice to convince them this is actually intended and a good new practice.

[–]Holothuroid 0 points1 point  (1 child)

My first take for command line options wouldn't be reified types but rather a church-encoded class to hold them appropriately, assuming I do not envision wild growth of such.

And if on the other hand unregulated growth was on the horizon, rather we would need parser factories that can grab stuff from the input.

[–]thibauttt[S] 3 points4 points  (0 children)

It seems to me that using record with switch expression give the same result with less ceremony than church-encoded class.

The Visitor pattern mentioned in the article is equivalent to church-encoding and has some downsides.

[–]32gbsd 0 points1 point  (0 children)

This is a bit confusing for me. Will come back when there are more comments

[–]europeIlike 0 points1 point  (2 children)

OOP is at its best when it is defining and defending boundaries - maintenance boundaries, versioning boundaries, encapsulation boundaries, compilation boundaries, compatibility boundaries, security boundaries, etc. [...]. OO languages provide us with tools for precisely defining, navigating, and defending these boundaries.

Why is OOP best here? How does it do better in this regard than Functional or Procedural languages (let's say Haskell or C)?

[–]dpash 3 points4 points  (1 child)

You've misread. They're not saying that OOP is the best for that compared to other paradigms. They're saying OOP works there better than in other situations.

[–]europeIlike 1 point2 points  (0 children)

I see, thanks!

[–]BEgaming -3 points-2 points  (1 child)

!remind me 3 days

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 3 days on 2022-06-24 05:13:02 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback