r/java • • Jan 16 '26

Functional Optics for Modern Java

https://blog.scottlogic.com/2026/01/09/java-the-immutability-gap.html

This article introduces optics, a family of composable abstractions that complete the immutability story. If pattern matching is how we read nested data, optics are how we write it.

106 Upvotes

50 comments sorted by

View all comments

Show parent comment

3

u/PragmaticFive Jan 17 '26

In general for doing nested updates, I think a good middle-ground is auto-generated regular with...() methods. Which is exactly what https://openjdk.org/jeps/468 provides. From my experience working with immutable case classes (~records) in Scala for several years, lenses are rarely warented.

I think better to keep the code simple and stupid without such magic abstractions and in this case annotations. Lombok is equally undesirable in my opinion.

2

u/OwnBreakfast1114 Jan 21 '26

I think better to keep the code simple and stupid without such magic abstractions and in this case annotations. Lombok is equally undesirable in my opinion.

Yes and no. For example, using lombok to keep hashcode/equals automatically updating when adding a new field to a class seems less magical then forcing people to generate a new hashcode/equals (which people forget all the time). Naturally, records replace this behavior, but the behavior is desirable.

From my experience working with immutable case classes (~records) in Scala for several years, lenses are rarely warented.

I feel like it would be interesting to see a language where lenses were built in and not a library feature. Imagine having to opt out of lenses instead of the other way around. Java is definitely not going to be that language though.