Prism

dev.constructive.eo.optics.Prism
object Prism

Constructors for Prism — the partial single-focus optic, backed by Either. "A Prism[S, A]" is prose shorthand for Optic[S, S, A, A, Either]: there is deliberately NO type Prism[S, A] alias to ascribe (Prism is only this constructor object) — TWO concrete classes back the family (MendTearPrism, and PickMendPrism from optional / pOptional), so an alias could only name the generic Optic, and ascribing that would knock code off the hot, fused paths (the fused compose overloads and capability mixins live on the concrete classes; contrast Iso, whose single concrete class does get an alias). Leave vals un-ascribed and let consuming signatures demand a capability (CanGetOption[S, A], CanReverseGet[S, A], …) instead. A prism encodes a branch of a sum type: getOption(s) succeeds when s matches, reverseGet(a) lifts back. The eo-generics module's prism[S, A] macro derives prisms on enums / sealed traits / union types.

'''Miss semantics (normative).''' A write through a missed prism is a silent identity pass-through: modify(f)(s) and replace(b)(s) return s unchanged (f is never invoked), and foldMap folds zero foci (returns Monoid.empty). No error, no signal. When hit-ness matters, probe with .getOption first.

Attributes

Source
Prism.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Prism.type

Members list

Grouped members

Constructors

def apply[S, A](getOrModify: S => Either[S, A], reverseGet: A => S): MendTearPrism[S, S, A, A]

Monomorphic constructor (S = T, A = B). getOrModify returns Right(a) on match and Left(s) on miss.

Monomorphic constructor (S = T, A = B). getOrModify returns Right(a) on match and Left(s) on miss.

Attributes

Example
enum Shape:
 case Circle(r: Double)
 case Square(s: Double)
val circleP = Prism[Shape, Shape.Circle](
 { case c: Shape.Circle => Right(c); case other => Left(other) },
 identity,
)
Source
Prism.scala
def optional[S, A](getOption: S => Option[A], reverseGet: A => S): PickMendPrism[S, A, A]

Option-shaped constructor — returns a PickMendPrism whose fused extensions avoid the intermediate Either the generic constructor must build. Prefer when the projection is already S => Option[A].

Option-shaped constructor — returns a PickMendPrism whose fused extensions avoid the intermediate Either the generic constructor must build. Prefer when the projection is already S => Option[A].

Attributes

Source
Prism.scala
def pOptional[S, A, B](getOption: S => Option[A], reverseGet: B => S): PickMendPrism[S, A, B]

Polymorphic counterpart to optional — type change on write.

Polymorphic counterpart to optional — type change on write.

Attributes

Source
Prism.scala
def pPrism[S, T, A, B](getOrModify: S => Either[T, A], reverseGet: B => T): MendTearPrism[S, T, A, B]

Polymorphic constructor — allows the miss branch to produce a different T. For refinement-style conversions; most code wants apply.

Polymorphic constructor — allows the miss branch to produce a different T. For refinement-style conversions; most code wants apply.

Attributes

Source
Prism.scala