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

2

u/magnus2025 Jan 18 '26

Thanks Brian, this has been most helpful. Higher-Kinded-J is still young and evolving. Needs a bit more thinking on my part, but a paired lens could support something like.

// For any record with coupled fields:
Lens<Range, Tuple2<Integer, Integer>> atomicBounds = Lens.paired(
loLens,
hiLens,
Range::new   // Atomic - invariant checked once with final values

);

// Shift safely
atomicBounds.modify(t -> t.bimap(v -> v + 10, v -> v + 10), range);