Optional

dev.constructive.eo.optics.Optional
See theOptional companion class
object Optional

Constructor for Optional — the conditionally-present single-focus optic, backed by Affine. An Optional[S, A] (short for Optic[S, S, A, A, Affine]) encodes a field that may or may not be there. Composes freely with Lens via cross-carrier .andThen (auto-morphs through Composer[Tuple2, Affine]).

Attributes

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

Members list

Grouped members

Constructors

def apply[S, T, A, B, F[_, _]](getOrModify: S => Either[T, A], reverseGet: ((S, B)) => T): Optional[S, T, A, B]

Construct an Optional from getOrModify (Right(a) on hit, Left(t) on miss) and reverseGet: (S, B) => T. The F parameter is unused — kept for constructor-shape symmetry.

Construct an Optional from getOrModify (Right(a) on hit, Left(t) on miss) and reverseGet: (S, B) => T. The F parameter is unused — kept for constructor-shape symmetry.

Attributes

Example
case class Person(age: Int, name: String)
val adultAge = Optional[Person, Person, Int, Int, Affine](
 getOrModify = p => Either.cond(p.age >= 18, p.age, p),
 reverseGet  = { case (p, a) => p.copy(age = a) },
)
Source
Optional.scala
def readOnly[S, A](matches: S => Option[A]): AffineFold[S, A]

Read-only Optional — T = Unit rules out .modify / .replace. Delegates to AffineFold.apply; see that constructor for the specialised X = (Unit, Unit) shape and its per-hit allocation savings.

Read-only Optional — T = Unit rules out .modify / .replace. Delegates to AffineFold.apply; see that constructor for the specialised X = (Unit, Unit) shape and its per-hit allocation savings.

Attributes

Example
case class Person(age: Int)
val adultAge: Optic[Person, Unit, Int, Int, Affine] =
 Optional.readOnly(p => Option.when(p.age >= 18)(p.age))
Source
Optional.scala
def selectReadOnly[A](p: A => Boolean): AffineFold[A, A]

Filtering read-only Optional — mirror of Fold.select over a single focus.

Filtering read-only Optional — mirror of Fold.select over a single focus.

Attributes

Source
Optional.scala