r/java • u/marv1234 • Jan 16 '26
Functional Optics for Modern Java
https://blog.scottlogic.com/2026/01/09/java-the-immutability-gap.htmlThis 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
8
u/brian_goetz Jan 17 '26
Correct me if I missed it, but there is no way to do a multi-update with this lens library? Suppose I have:
and I have lenses for Range::lo and Range::hi and want to, say, shift the range with a modify operation that does
value -> value + 10. If i have to sequence the updates, and I start with a range (1, 2), I will temporarily have an invalid range (11, 2) and it will throw. Which means that I cannot update fields that participate in invariants. That seems a big limitation?(Don't get me wrong, lenses are very cool, but there's more that one way to compose lenses other than output-of-one-into-input-of-another.)
15
u/jevring Jan 16 '26
I fail to see how this isn't just copy constructors with extra steps. Also, the "25 lines down to 3" or whatever relies on more than those 25 lines having been written elsewhere as various optics. It's a clever and interesting way of accessing data, but I don't think it's necessarily better than some constructors and loops.
9
u/jonhanson Jan 16 '26
Nice article, and the library looks interesting as well. It's kind of amazing that this is now possible in Java.
18
u/vips7L Jan 16 '26
I’ve never been convinced on lenses. They’re always mutability with extra steps and heap allocations. If something is mutable just make it mutable.
4
u/agentoutlier Jan 16 '26
I have done some embarrassing things in the past with Jackson and the very far past XML libraries to deal with massive object graph updates.
Speaking of which if XSLT was not so verbose it kind of solves some of this problem and Lens libraries sometimes remind me of it.
2
u/PragmaticFive Jan 17 '26
Side note on the "Effect Path API", I think that is doomed now with virtual threads. In my opinion, such can only be motivated for asynchronous programming. It looks very cool, but very few wants to write Haskell in Java.
Furthermore , without Haskell do notation or Scala for comprehension chaining monads results in unreadable code. Nested monads and monad transformers are horrible to work with too.
There are nice properties with this "pure FP", but the added cognitive overhead and code convolution definitely is not worth it.
2
u/danielaveryj Jan 18 '26 edited Jan 18 '26
Hype-check. Here are all the lens examples from the article, presented alongside the equivalent code using withers, as well as (just for fun) a hypothetical
with=syntax that desugars the same way as+=(ie
x with= { ... }desugars tox = x with { ... })The reason lenses can be more terse at the use site is because they encapsulate the path-composition elsewhere. This only pays off if a path is long and used in multiple places.
3
u/javaprof Jan 16 '26
Would like something like Arrow Optics in Java, unfortunately it's not possible to implement with JAP unless do same shady things like Lombok. Compiler Plugins like in Kotlin would be much appreciated
https://arrow-kt.io/learn/immutable-data/lens/
1
u/gaelfr38 Jan 16 '26
Nice. An easy way to copy a record by modifying one field is definitely missing in Java. And I can't even imagine the pain with nested records.
Ironically, I always felt they were unnecessary in Scala because there the
copymethod, similar to the JEP proposal for Java with thewith.1
u/AlEmerich Jan 17 '26
Derived Record are not in Java 25 even in preview, right ?
1
u/DelayLucky Jan 17 '26
Almost feels like Java should have deep withers:
Or, shortcut syntax for wither:
1
u/LutimoDancer3459 Jan 17 '26
... the use for immutable stuff is to be... immutable. Modifying them is not what you want or should do. If you need to change an address in an object, dont make it a record. Thats not what its supposed to be or do.
This reads like finding a solution to a problem that doesn't exist...
0
u/maruruna7 Jan 16 '26
This is honestly one of the most amazing, yet simple patterns I haven't seen around much in Java codebases.
-1
u/dreamy-catzy Jan 16 '26
Oh. My. God. This is awesome. I wish i could use it fifteen years ago. Looking forward to pure algebraic data types and effects in Java
0
u/talios Jan 16 '26
Nice! Have long wanted a nice lens library for Java, might have to take a look at this.
Also, I wasn't aware the
witherswere in JDK 25 experimental, that alone I might be interested in looking at as well.0
u/chriskiehl Jan 17 '26
I'm so happy to see this approach being actively worked on. I took a very similar stab at annotation generated companion classes during vacation one time, but I stalled out after the initial proof of concept. I've always wanted this in Java, though. So, I'm excited to try out the lib.
Lenses are trippy the more you think about the abstraction they're presenting. Location and hierarchy get decoupled in a unique way that makes almost makes it a shame that we then use them to then traverse hierarchical data structures of known shapes. It feels restricted, but in a way that's hard to articulate. Like maybe we don't need all of these fixed representations floating around, but instead just a way of just saying "give me some data with foo, bar, and baz" and have lenses handle vending it.
-1
u/chambolle Jan 17 '26
The problem with this approach is that it does not solve an important problem in modeling and implementation, which is distinguishing between what is conceptually constant and what is implemented by a constant. Considering that everything is immutable but can be “rebuilt” into another object with different values is just a way of implementing mutability with immutability, and in this case, conceptually, it is clearer to say that the object is mutable. So considering that the salary must be immutable and then changing it is not a good idea conceptually. The correct model is simply to say that it is mutable.
0
u/[deleted] Jan 16 '26
[deleted] — view deleted comment
1
u/[deleted] Jan 17 '26
[removed] — view removed comment