Live data from Hacker News

Monocle: Optics Library for Scala

optics.dev

1–10 of 70 posts

Re: Monocle: Optics Library for Scala

#5

I haven't encountered this pattern before. Is there some more information on what problems this is designed to solve?

If monads are programmable semicolons (ways to chain operation), lenses are programmable dots (ways to delegate access to data). Other optics are largely generalizations of that pattern.

Re: Monocle: Optics Library for Scala

#7
So behind the scenes, every one of those statements will make a whole new user object with a whole new address object so that it remains immutable? And whether that will actually have any real-world performance impact is I guess entirely situational. Still, what happens if you do that with a big object graph?

Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?

Re: Monocle: Optics Library for Scala

#9
post #7

So behind the scenes, every one of those statements will make a whole new user object with a whole new address object so that it remains immutable? And whether that will actually have any real-world performance impact is I guess entirely situational. Still, what happens if you do that with a big object graph? Also, the original strong need for immutable data in the first place is safety under concurrency and parallel…

This is in general how "mutations" are supposed to be done in a language like Scala (and is not unique to this library). Yes, Scala does have a set of mutable collections, but the immutable collections are heavily optimized to make creating a "new" collection with a mutation much cheaper than having to copy the entire collection.

Of course, copying a case class in order to change a field likely does require a full copy of the object, though since this is the JVM, things like strings can be shared between them.

Ultimately this pattern is... fine. Most uses don't end up caring about the extra overhead vs. that of direct mutation. I don't recall if the Scala compiler does this, but another optimization that can be used is to actually mutate an immutable object when the compiler knows the original copy isn't used anywhere else after the mutation.

> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?

That's one of the uses, but multiple ownership in general is another, without the presence of concurrency.

On top of that, there's the general belief (which I subscribe to) that mutation introduces higher cognitive load on someone understanding the code. Immutable data is much easier to reason about.

Re: Monocle: Optics Library for Scala

#10

I haven't encountered this pattern before. Is there some more information on what problems this is designed to solve?

Lens are the functional version of getters and setters. A lens takes a product type (struct, class, etc) and allows you to view or update part of it. Prisms are something similar for sum types (variants) that allow you to look at a value if it's present and err otherwise.

The optical analogy comes from how these operations resemble zooming in on structures with a magnifying glass and the entire family of related transformations is called optics.

Post reply on HN