Exit e-book
Show all chapters
Coming soon to ZIO Optics
Coming soon to ZIO Optics
Improve your focus with ZIO optics

Coming soon to ZIO Optics

So far we’ve seen that ZIO Optics already provides lots of nice tools that developers can use to improve their productivity. However, ZIO Optics is still in the development stage, so there is even more great stuff coming in the future:

  • Automatic generation of optics for any ADT: In the previous examples you can see that, if you want the power of optics, you will have to write basic optics (such as Lenses or Prisms) for all of the fields of your product/sum types. That means the more fields and ADTs you have, the more optics you’ll need to write. The good news is that, in future releases, ZIO Optics will automatically derive these for you, so you won’t have to write them yourself!

Dot syntax: Previously we have seen how ZIO Optics adds extension methods to the ZRef data type from ZIO.  This allows us to work with nice dot syntax, like this:

ref.key("key").right.at(0).update(_ + 1)

In future releases, it should be possible to have a similar dot syntax for ordinary values, as shown in the ZIO Optics documentation:

val map: Map[String, Either[String, Chunk[Int]]] = ???

val updated: Either[OpticFailure, Map[String, Either[String, Chunk[Int]]]] =
  map.optic.key("key").right.at(0).update(_ + 1)

Moreover, when automatic derivation of optics is introduced, this syntax will be supported for user defined data structures as well.

  • Constructors and operators for other types of optics that can be derived from the basic Optic type: Getter, Setter, Fold. If you want to get a basic idea about these, you can take a look at the ZIO Optics documentation.

More constructors and operators for Lens, Prism, Iso, Optional and Traversal.