Skip to main content
Version: 2.0.x

Constructing Optics

ZIO Optics comes included with a variety of optics for working with data structures defined in the Scala standard library.

All of these are available in the Optic companion object. For example, if we want to access the first value in a (String, Int) we could do:

import zio.optics._

val first: Lens[(String, Int), String] =

In addition, constructors are available in the companion object of each optic. So if we wanted to access the right value of an Either we could also do it as:

val right: Prism[Either[String, Int], Int] =

To create optics for your own data types you just need to define a getter and a setter. For instance, we can create an optic that accesses the age of a person like this:

case class Person(name: String, age: Int)

object Person {
val age: Lens[Person, Int] =
person => Right(person.age),
age => person => Right(person.copy(age = age))

If your optic fails you should return a Left with an OpticFailure. An OpticFailure allows you to provide an error message which you can use to describe why the optic failed.

In the future ZIO Optics will derive these optics automatically so you will not have to do this yourself.