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]).
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) },
)
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))