Prism

zio.blocks.schema.Prism
See thePrism companion object
sealed trait Prism[S, A <: S] extends Optic[S, A]

A prism focuses on one case A of a sealed trait, enum or other variant S. On a value of another case, getOption returns None, and replace and modify return the value unchanged. The *OrFail methods return an OpticCheck.UnexpectedCase instead, which names the expected and the actual case.

sealed trait Format
case class Paperback(pages: Int) extends Format
case class Ebook(sizeMb: Double) extends Format
object Format extends CompanionOptics[Format] {
 implicit val schema: Schema[Format] = Schema.derived
 val paperback: Prism[Format, Paperback] = $(_.when[Paperback])
}

Format.paperback.getOption(Paperback(870))  // Some(Paperback(870))
Format.paperback.getOption(Ebook(5.2))      // None
Format.paperback.reverseGet(Paperback(870)) // Paperback(870), typed as a Format

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 s as the case A when it is that case, and None otherwise.

Returns s as the case A when it is that case, and None otherwise.

sealed trait Format
case class Paperback(pages: Int) extends Format
case class Ebook(sizeMb: Double) extends Format
object Format extends CompanionOptics[Format] {
 implicit val schema: Schema[Format] = Schema.derived
 val paperback: Prism[Format, Paperback] = $(_.when[Paperback])
}

Format.paperback.getOption(Paperback(870)) // Some(Paperback(870))
Format.paperback.getOption(Ebook(5.2))     // None

Attributes

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

Returns a when s is the case A, and s unchanged otherwise.

Returns a when s is the case A, and s unchanged otherwise.

sealed trait Format
case class Paperback(pages: Int) extends Format
case class Ebook(sizeMb: Double) extends Format
object Format extends CompanionOptics[Format] {
 implicit val schema: Schema[Format] = Schema.derived
 val paperback: Prism[Format, Paperback] = $(_.when[Paperback])
}

Format.paperback.replace(Paperback(870), Paperback(920)) // Paperback(920)
Format.paperback.replace(Ebook(5.2), Paperback(920))     // Ebook(5.2)

Attributes

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

Like replace, but returns None when s is another case.

Like replace, but returns None when s is another case.

Attributes

Source
Optic.scala
def reverseGet(a: A): S

Returns a as a value of the variant S. It never fails, because a case is always a value of its variant.

Returns a as a value of the variant S. It never fails, because a case is always a value of its variant.

Attributes

Source
Optic.scala

Concrete methods

def apply[B <: A](that: Prism[A, B]): Prism[S, B]

Composes this prism with that prism. The result is a Prism.

Composes this prism with that prism. The result is a Prism.

Attributes

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

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

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

Attributes

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

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

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

Attributes

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

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

Composes this prism 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.UnexpectedCase when s is another case, for example During attempted access at .when[Paperback], encountered an unexpected case at .when[Paperback]: expected Paperback, but got Ebook. For a composed prism, it names the first step that does not match.

Like getOption, but returns Left with an OpticCheck.UnexpectedCase when s is another case, for example During attempted access at .when[Paperback], encountered an unexpected case at .when[Paperback]: expected Paperback, but got Ebook. For a composed prism, it names the first step that does not match.

Attributes

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

Like replace, but returns Left with an OpticCheck.UnexpectedCase when s is another case.

Like replace, but returns Left with an OpticCheck.UnexpectedCase when s is another case.

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