Hyperlinkv0.8.0-beta.28

Optic

Optic.somefunctioneffect/Optic.ts:1549
<A>(): Prism<Option.Option<A>, A>

Prism that focuses on the value inside Option.Some.

When to use

Use when you have an Option<A> and want to read/update the inner value only when it is Some.

Details

  • getResult fails with an error message when the option is None.
  • set(a) wraps a in Option.some(a).

Example (Accessing Some value)

import { Optic, Option, Result } from "effect"

const _some = Optic.id<Option.Option<number>>().compose(Optic.some())

console.log(Result.isSuccess(_some.getResult(Option.some(42))))
// Output: true

console.log(Result.isFailure(_some.getResult(Option.none())))
// Output: true

console.log(_some.set(10))
// Output: { _tag: "Some", value: 10 }
Source effect/Optic.ts:154911 lines
export function some<A>(): Prism<Option.Option<A>, A> {
  const run = runRefinement(Option.isSome, { expected: "a Some value" })
  return makePrism(
    (s) =>
      Result.mapBoth(run(s), {
        onFailure: String,
        onSuccess: (s) => s.value
      }),
    Option.some
  )
}