effect-io-ai

Package: effect
Module: Optic

Optic.Prism

Focuses on a part A of S that may not be present (e.g. a union variant or a validated subset).

When to use

Use when the focus is conditional — reading can fail (wrong variant, failed validation).

Details

Example (Narrowing a tagged union)

import { Optic, Result } from "effect"

type Shape =
  | { readonly _tag: "Circle"; readonly radius: number }
  | { readonly _tag: "Rect"; readonly width: number }

const _circle = Optic.id<Shape>().tag("Circle")

console.log(Result.isSuccess(_circle.getResult({ _tag: "Circle", radius: 5 })))
// Output: true

console.log(Result.isFailure(_circle.getResult({ _tag: "Rect", width: 10 })))
// Output: true

See

Signature

export interface Prism<in out S, in out A> extends Optional<S, A> {
  readonly set: (a: A) => S
}

Source

Since v4.0.0