Optional

zio.blocks.schema.Optional
See theOptional companion object
sealed trait Optional[S, A] extends Optic[S, A]

An optional focuses on a part of S that may be missing: a field inside one case of a variant, the element at an index of a sequence, the value at a key of a map, or the value inside a wrapper. Composing a Lens with a Prism, in either order, gives an optional.

When the part is missing, getOption returns None, and replace and modify return a value equal to s. The *OrFail methods say why in an OpticCheck: another case (OpticCheck.UnexpectedCase), an index past the end (OpticCheck.SequenceIndexOutOfBounds), a missing key (OpticCheck.MissingKey), or a wrapper whose wrap or unwrap throws a SchemaError (OpticCheck.WrappingError). replace never adds a missing element or key.

case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
 implicit val schema: Schema[Book] = Schema.derived
 val firstTag: Optional[Book, String] = $(_.tags.at(0))
}

Book.firstTag.getOption(Book("Zionomicon", List("zio", "scala"))) // Some(zio)
Book.firstTag.getOption(Book("Zionomicon", Nil))                  // None

Attributes

See also
Companion
object
Source
Optic.scala
Graph
Supertypes
trait Optic[S, A]
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def getOption(s: S): Option[A]

Returns the part of s that this optional focuses on, or None when it is missing.

Returns the part of s that this optional focuses on, or None when it is missing.

case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
 implicit val schema: Schema[Book] = Schema.derived
 val firstTag: Optional[Book, String] = $(_.tags.at(0))
}

Book.firstTag.getOption(Book("Zionomicon", List("zio", "scala"))) // Some(zio)
Book.firstTag.getOption(Book("Zionomicon", Nil))                  // None

Attributes

Source
Optic.scala
def replace(s: S, a: A): S

Returns a copy of s with the part this optional focuses on replaced by a. When the part is missing, or a wrapped step on the path throws a SchemaError, the result equals s.

Returns a copy of s with the part this optional focuses on replaced by a. When the part is missing, or a wrapped step on the path throws a SchemaError, the result equals s.

case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
 implicit val schema: Schema[Book] = Schema.derived
 val firstTag: Optional[Book, String] = $(_.tags.at(0))
}

Book.firstTag.replace(Book("Zionomicon", List("zio", "scala")), "effects") // Book(Zionomicon,List(effects, scala))
Book.firstTag.replace(Book("Zionomicon", Nil), "effects")                  // Book(Zionomicon,List())

Attributes

Source
Optic.scala
def replaceOption(s: S, a: A): Option[S]

Like replace, but returns None when the part is missing or a wrapped step on the path throws a SchemaError.

Like replace, but returns None when the part is missing or a wrapped step on the path throws a SchemaError.

Attributes

Source
Optic.scala
def replaceOrFail(s: S, a: A): Either[OpticCheck, S]

Like replace, but returns Left with an OpticCheck when the part is missing, or with an OpticCheck.WrappingError when a wrapped step on the path throws a SchemaError.

Like replace, but returns Left with an OpticCheck when the part is missing, or with an OpticCheck.WrappingError when a wrapped step on the path throws a SchemaError.

Attributes

Source
Optic.scala

Concrete methods

def apply[B](that: Lens[A, B]): Optional[S, B]

Composes this optional with that lens. The result is an Optional.

Composes this optional with that lens. The result is an Optional.

Attributes

Source
Optic.scala
def apply[B <: A](that: Prism[A, B]): Optional[S, B]

Composes this optional with that prism. The result is an Optional.

Composes this optional with that prism. The result is an Optional.

Attributes

Source
Optic.scala
def apply[B](that: Optional[A, B]): Optional[S, B]

Composes this optional with that optional. The result is an Optional.

Composes this optional with that optional. The result is an Optional.

Attributes

Source
Optic.scala
def apply[B](that: Traversal[A, B]): Traversal[S, B]

Composes this optional with that traversal. The result is a Traversal.

Composes this optional with that traversal. The result is a Traversal.

Attributes

Source
Optic.scala
def getOrFail(s: S): Either[OpticCheck, A]

Like getOption, but returns Left with an OpticCheck that says why the part is missing, for example During attempted access at .tags.at(0), encountered a sequence out of bounds at .tags.at(0): index is 0, but size is 0.

Like getOption, but returns Left with an OpticCheck that says why the part is missing, for example During attempted access at .tags.at(0), encountered a sequence out of bounds at .tags.at(0): index is 0, but size is 0.

Attributes

Source
Optic.scala

Inherited methods

final def !=(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

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
final def !=(that: Optic[S, A]): SchemaExpr[S, Boolean]

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
final def %(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

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
final def &(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def &&(that: Boolean)(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

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
final def &&(that: Optic[S, A])(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

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
final def *(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

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
final def +(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

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
final def -(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

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
final def /(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

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
final def <(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

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
final def <(that: Optic[S, A]): SchemaExpr[S, Boolean]

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
final def <<(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def <=(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

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
final def <=(that: Optic[S, A]): SchemaExpr[S, Boolean]

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
final def ===(that: Optic[S, A]): SchemaExpr[S, Boolean]

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
final def ===(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

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
final def >(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

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
final def >(that: Optic[S, A]): SchemaExpr[S, Boolean]

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
final def >=(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

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
final def >=(that: Optic[S, A]): SchemaExpr[S, Boolean]

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
final def >>(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def >>>(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def ^(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def asEquivalent[B](implicit ev: A =:= B): Optic[S, B]

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
final def concat(that: String)(implicit ev: A =:= String): SchemaExpr[S, String]

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
final def length(implicit ev: A =:= String): SchemaExpr[S, Int]

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
final def listValues[B](implicit ev: A =:= List[B]): Traversal[S, B]

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 List sequence schema

Inherited from:
Optic
Source
Optic.scala
final def matches(that: String)(implicit ev: A =:= String): SchemaExpr[S, Boolean]

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
final def pow(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

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
final def setValues[B](implicit ev: A =:= Set[B]): Traversal[S, B]

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 Set sequence schema

Inherited from:
Optic
Source
Optic.scala
final def unary_!(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

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
final def unary_~(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def vectorValues[B](implicit ev: A =:= Vector[B]): Traversal[S, B]

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 Vector sequence schema

Inherited from:
Optic
Source
Optic.scala
final def |(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

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
final def ||(that: Boolean)(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

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
final def ||(that: Optic[S, A])(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

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

def check(s: S): Option[OpticCheck]

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
def focus: Bound[A]

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
def modify(s: S, f: A => A): S

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
def modifyOption(s: S, f: A => A): Option[S]

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
def modifyOrFail(s: S, f: A => A): Either[OpticCheck, S]

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
def source: Bound[S]

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