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]" is prose shorthand for Optic[S, S, A, A, Affine] — there is NO two-parameter alias to ascribe; the concrete Optional class below always takes all four parameters (Optional[S, S, A, A] for the monomorphic case). Ascribing the concrete class is fine — that keeps code on the hot, fused paths; what would knock it off is ascribing the generic Optic[…], which drops the fused compose overloads and capability mixins for generic dispatch. Optional.apply returns that concrete class, so leave vals un-ascribed (or name the four-parameter class) and let consuming signatures demand a capability (CanGetOption[S, A], CanModify[S, A], …) instead. An optional encodes a field that may or may not be there. Composes freely with Lens via cross-carrier .andThen (auto-morphs through Composer[Tuple2, Affine]).

'''Miss semantics (normative).''' A write through a missed optional 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

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](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.

Construct an Optional from getOrModify (Right(a) on hit, Left(t) on miss) and reverseGet: (S, B) => T.

Attributes

Example
case class Person(age: Int, name: String)
val adultAge = Optional[Person, Person, Int, Int](
 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]): PickFold[S, A]

Read-only Optional — T = Unit rules out .modify / .replace. Delegates to AffineFold.apply; see PickFold 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 PickFold for the specialised X = (Unit, Unit) shape and its per-hit allocation savings.

Attributes

Example
case class Person(age: Int)
val adultAge = Optional.readOnly(p => Option.when(p.age >= 18)(p.age))
Source
Optional.scala
def selectReadOnly[A](p: A => Boolean): PickFold[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