A lens focuses on a part of S that is always there, such as a field of a case class, or a field of a field. Use it to read or replace that part without rebuilding the record by hand. A lens never reports a missing focus: check always returns None, modifyOption always returns Some, and modifyOrFail always returns Right.
Get a lens from the $ macro of CompanionOptics, from DerivedOptics, from Reflect.Record#lensByName, or with the Lens.apply constructor.
case class Book(title: String, year: Int)
object Book extends CompanionOptics[Book] {
implicit val schema: Schema[Book] = Schema.derived
val year: Lens[Book, Int] = $(_.year)
}
val zionomicon = Book("Zionomicon", 2021)
Book.year.get(zionomicon) // 2021
Book.year.replace(zionomicon, 2024) // Book(Zionomicon,2024)
Attributes
- See also
- Companion
- object
- Source
- Optic.scala
- Graph
-
- Supertypes
Members list
Value members
Abstract methods
Returns the part of s that this lens focuses on.
Returns the part of s that this lens focuses on.
case class Book(title: String, year: Int)
object Book extends CompanionOptics[Book] {
implicit val schema: Schema[Book] = Schema.derived
val title: Lens[Book, String] = $(_.title)
}
Book.title.get(Book("Zionomicon", 2021)) // "Zionomicon"
Attributes
- Source
- Optic.scala
Returns a copy of s with the part this lens focuses on replaced by a.
Returns a copy of s with the part this lens focuses on replaced by a.
case class Book(title: String, year: Int)
object Book extends CompanionOptics[Book] {
implicit val schema: Schema[Book] = Schema.derived
val title: Lens[Book, String] = $(_.title)
}
Book.title.replace(Book("Programming in Scala", 2021), "Programming in Scala, Fifth Edition")
// Book(Programming in Scala, Fifth Edition,2021)
Attributes
- Source
- Optic.scala
Inherited methods
Builds a SchemaExpr that is true where the focus of this optic differs from the literal that.
Builds a SchemaExpr that is true where the focus of this optic differs from the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic differs from the focus of that.
Builds a SchemaExpr that is true where the focus of this optic differs from the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the remainder of this optic's numeric focus divided by the literal that.
Builds a SchemaExpr for the remainder of this optic's numeric focus divided by the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the bitwise and of this optic's integral focus and the literal that.
Builds a SchemaExpr for the bitwise and of this optic's integral focus and the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the logical and of this optic's Boolean focus and the literal that.
Builds a SchemaExpr for the logical and of this optic's Boolean focus and the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the logical and of this optic's Boolean focus and the focus of that.
Builds a SchemaExpr for the logical and of this optic's Boolean focus and the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's numeric focus times the literal that.
Builds a SchemaExpr for this optic's numeric focus times the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's numeric focus plus the literal that.
Builds a SchemaExpr for this optic's numeric focus plus the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's numeric focus minus the literal that.
Builds a SchemaExpr for this optic's numeric focus minus the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's numeric focus divided by the literal that.
Builds a SchemaExpr for this optic's numeric focus divided by the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is less than the literal that.
Builds a SchemaExpr that is true where the focus of this optic is less than the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is less than the focus of that.
Builds a SchemaExpr that is true where the focus of this optic is less than the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's integral focus shifted left by the literal that.
Builds a SchemaExpr for this optic's integral focus shifted left by the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the literal that.
Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the focus of that.
Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic equals the focus of that.
Builds a SchemaExpr that is true where the focus of this optic equals the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic equals the literal that. Evaluate it on an S with SchemaExpr#eval.
Builds a SchemaExpr that is true where the focus of this optic equals the literal that. Evaluate it on an S with SchemaExpr#eval.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is greater than the literal that.
Builds a SchemaExpr that is true where the focus of this optic is greater than the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is greater than the focus of that.
Builds a SchemaExpr that is true where the focus of this optic is greater than the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the literal that.
Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the focus of that.
Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, keeping the sign.
Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, keeping the sign.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, filling with zeros.
Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, filling with zeros.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the bitwise exclusive or of this optic's integral focus and the literal that.
Builds a SchemaExpr for the bitwise exclusive or of this optic's integral focus and the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Returns this optic with its focus type changed to B, which ev proves is the same type. The optic is cast, not copied.
Returns this optic with its focus type changed to B, which ev proves is the same type. The optic is cast, not copied.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's String focus followed by the literal that.
Builds a SchemaExpr for this optic's String focus followed by the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the length of this optic's String focus.
Builds a SchemaExpr for the length of this optic's String focus.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Composes this optic, whose focus is a List, with a traversal over every element of that list.
Composes this optic, whose focus is a List, with a traversal over every element of that list.
Attributes
- Throws
-
java.lang.RuntimeException
("Expected List") if the focus schema is not a
Listsequence schema - Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr that is true where this optic's String focus matches the Java regular expression that as a whole (java.util.regex.Pattern.matches).
Builds a SchemaExpr that is true where this optic's String focus matches the Java regular expression that as a whole (java.util.regex.Pattern.matches).
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for this optic's numeric focus raised to the power of the literal that.
Builds a SchemaExpr for this optic's numeric focus raised to the power of the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Composes this optic, whose focus is a Set, with a traversal over every element of that set.
Composes this optic, whose focus is a Set, with a traversal over every element of that set.
Attributes
- Throws
-
java.lang.RuntimeException
("Expected Set") if the focus schema is not a
Setsequence schema - Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the negation of this optic's Boolean focus.
Builds a SchemaExpr for the negation of this optic's Boolean focus.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the bitwise complement of this optic's integral focus.
Builds a SchemaExpr for the bitwise complement of this optic's integral focus.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Composes this optic, whose focus is a Vector, with a traversal over every element of that vector.
Composes this optic, whose focus is a Vector, with a traversal over every element of that vector.
Attributes
- Throws
-
java.lang.RuntimeException
("Expected Vector") if the focus schema is not a
Vectorsequence schema - Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the bitwise or of this optic's integral focus and the literal that.
Builds a SchemaExpr for the bitwise or of this optic's integral focus and the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the logical or of this optic's Boolean focus and the literal that.
Builds a SchemaExpr for the logical or of this optic's Boolean focus and the literal that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Builds a SchemaExpr for the logical or of this optic's Boolean focus and the focus of that.
Builds a SchemaExpr for the logical or of this optic's Boolean focus and the focus of that.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Inherited and Abstract methods
Returns None when this optic reaches its focus in s, and otherwise an OpticCheck that says why not. The possible problems are another case of a variant, a wrapper whose unwrap throws a SchemaError (both errors), an index past the end, a missing map key, an empty collection, and, for searchFor, no match at all (all four warnings). Except for searchFor, which decodes every candidate with its schema, check never runs a wrapper's wrap, so it does not see failures that only replace or modify meet.
Returns None when this optic reaches its focus in s, and otherwise an OpticCheck that says why not. The possible problems are another case of a variant, a wrapper whose unwrap throws a SchemaError (both errors), an index past the end, a missing map key, an empty collection, and, for searchFor, no match at all (all four warnings). Except for searchFor, which decodes every candidate with its schema, check never runs a wrapper's wrap, so it does not see failures that only replace or modify meet.
A Lens always returns None. A Prism and an Optional report the first problem on the path. A Traversal reports the problems in every branch it visits, even when it reaches other parts: atIndices(0, 2) on a one-element list reports index 2.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
The schema of the parts this optic focuses on. For a composed optic, it is the focus of the last step.
The schema of the parts this optic focuses on. For a composed optic, it is the focus of the last step.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Returns s with f applied to every part this optic focuses on. When there is nothing to focus on, the result equals s: a Prism on another case, an Optional on a missing index or key, a Traversal over an empty collection. It also equals s when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md); other exceptions, from the path or from f, propagate. modifyOption and modifyOrFail say when nothing was modified.
Returns s with f applied to every part this optic focuses on. When there is nothing to focus on, the result equals s: a Prism on another case, an Optional on a missing index or key, a Traversal over an empty collection. It also equals s when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md); other exceptions, from the path or from f, propagate. modifyOption and modifyOrFail say when nothing was modified.
case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
implicit val schema: Schema[Book] = Schema.derived
val tags: Traversal[Book, String] = $(_.tags.each)
}
Book.tags.modify(Book("Zionomicon", List("zio", "scala")), _.toUpperCase)
// Book(Zionomicon,List(ZIO, SCALA))
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Like modify, but returns None instead of an unchanged value when there is nothing to focus on, and when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Some.
Like modify, but returns None instead of an unchanged value when there is nothing to focus on, and when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Some.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Like modify, but returns Left with an OpticCheck that says why nothing was modified, as check does, and a OpticCheck.WrappingError when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Right. A Traversal returns Right once it has modified at least one part, even when other parts are missing; check reports those.
Like modify, but returns Left with an OpticCheck that says why nothing was modified, as check does, and a OpticCheck.WrappingError when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Right. A Traversal returns Right once it has modified at least one part, even when other parts are missing; check reports those.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
The schema of the value this optic starts from. For a composed optic, it is the source of the first step.
The schema of the value this optic starts from. For a composed optic, it is the source of the first step.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala
Returns the path of this optic as a DynamicOptic, with one node per step. For example, $(_.format.when[Paperback].pages).toDynamic.toString is .format.pages.
Returns the path of this optic as a DynamicOptic, with one node per step. For example, $(_.format.when[Paperback].pages).toDynamic.toString is .format.pages.
Attributes
- Inherited from:
- Optic
- Source
- Optic.scala