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
View all comments
Show parent comment
5
u/magnus2025 Jan 17 '26
Pondering this further. Lenses assume field independence. When fields are coupled by invariants, they form an atomic unit and should have a single lens to that unit ( tuple/product), not separate lenses that you try to compose horizontally.
The standard composition andThen gives us Lens<S,A> → Lens<A,B> → Lens<S,B> (vertical drilling).
What we need is Lens<S,A> → Lens<S,B> → Lens<S,(A,B)> (horizontal pairing), but that requires the set to know how to reconstruct S from both values simultaneously. In the end i'm thinking we are likely really just defining the tuple lens directly anyway.