Minimizing the boilerplate

The preceding example showed quite a lot of boilerplate code. It is not complicated, but it requires us to write quite a lot of extra stuff, and then any refactoring will likely affect these manually defined lenses. There has been an effort in creating libraries that automatically generate lenses for all user-defined classes that can then be used easily. One example of a library that seems to be maintained well is Monocle: https://github.com/julien-truffaut/Monocle. It is well-documented and can be used so that we don't have to write any boilerplate code. It has its limitations though, and users should make sure they are okay with what the library provides. It also provides other optics concepts that could be useful. ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.