Design your own Optics to Improve Composability and Testability
Optics, such as lenses and prisms, are accessors / mutators for data structures, that can be elegantly composed to access / mutate positions deep within nested data structures. For some of our real world use cases we were unable to take full advantage of the composability of optics. In one case we could compose a sequence of optics to access a desired position in a data structure, but the same optic couldn’t mutate the position in the way we desired. We also found that composing a sequence of optics that each optionally access a position would obscure which optic in the sequence failed – which we needed to know in practice for error-reporting purposes. Consequently we were forced to “manually” compose optics for the desired behaviour, and the result was far less elegant or easily testable than composing sequences of optics. That is, until we wised up and designed our own composable, testable, optics that behaved the way we wanted.
This talk is a case study of how we improved the composability and testability of our components by designing optics with the behaviour we desired. We’ll walk through a simplified version of the problem we were trying to solve, the limitations we faced with vanilla optics, and how we solved the problem more elegantly by designing our own optics. The optics will be open sourced as a Scala library that complements the monocle library of optics by Julien Truffaut, et al.