by ImprobableTruth · 3y ago · 0 comments
Lenses are "smart" in the sense that they only copy what's necessary.
From the perspective of such a programmer, I can’t think of a scenario where I’d want both value and reference equality semantics for the same objects. Is it reasonable to assume that the “smartness” here is likewise focused on making immutability perform well, rather than on use cases where value and reference equality are simultaneous considerations?
1: Handwaves away implementation details. General cautions about abstractions leaking apply.
2: Exceptions may apply[1].
And you're close, it's about using immutability to make value equality cheap by making reference equality a proxy for it. If you don't use mutations, value equality implies reference equality and is thus equivalent (since reference equality already normally implies value equality). That means you can get away with just a single pointer comparison in comparison to completely traversing both structures. This is e.g. what React does to determine whether arguments of a component have changed.
(Well, at least I also struggle to think of a scenario where both types of equality are semantically important).
pendingOrders.map(order -> order.approvalConfirmationUpdated(now()) record Order(List<Approval> approvals, int version, OrderStatus status) {
Order confirmed(Instant timestamp, UUID approver) {
var approvals = this.approvals.stream().map(a-> a.user().uuid().equals(approver) ? a.confirmed(timestamp) : a).toList();
return new Order(approvals, version++, OrderStatus.APPROVED);
}