Prism<S, A>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).
- Building a new
SfromAdoes not require the originalS.
Details
getResult(s)returnsResult.Success<A>when the focus matches, orResult.Failure<string>with an error message.set(a)always succeeds and returns a newS.- Extends Optional.
- Composing two Prisms produces a Prism; composing a Prism with a Lens produces an Optional.
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: trueexport interface interface Prism<in out S, in out A>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).
- Building a new
S from A does not require the original S.
Details
getResult(s) returns Result.Success<A> when the focus matches, or
Result.Failure<string> with an error message.
set(a) always succeeds and returns a new S.
- Extends
Optional
.
- Composing two Prisms produces a Prism; composing a Prism with a
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
Prism<in out function (type parameter) S in Prism<in out S, in out A>S, in out function (type parameter) A in Prism<in out S, in out A>A> extends interface Optional<in out S, in out A>The most general optic — both reading and writing can fail.
When to use
Use when the focus may not exist in S and writing a new A back may also
fail, for example when the source no longer matches the expected shape. This
is the base type extended by
Iso
,
Lens
,
Prism
, and
Details
getResult(s) returns Result.Success<A> or Result.Failure<string>.
replaceResult(a, s) returns Result.Success<S> or
Result.Failure<string>.
replace(a, s) returns the original s on failure (never throws).
modify(f) returns the original s on failure (never throws).
- All operations are pure; inputs are never mutated.
Example (Focusing on an optional record key)
import { Optic, Result } from "effect"
type Env = { [key: string]: string }
const _home = Optic.id<Env>().at("HOME")
console.log(Result.isSuccess(_home.getResult({ HOME: "/root" })))
// Output: true
console.log(Result.isFailure(_home.getResult({ PATH: "/bin" })))
// Output: true
// replace returns original on failure
console.log(_home.replace("/new", { PATH: "/bin" }))
// Output: { PATH: "/bin" }
Optional<function (type parameter) S in Prism<in out S, in out A>S, function (type parameter) A in Prism<in out S, in out A>A> {
readonly Prism<in out S, in out A>.set: (a: A) => Sset: (a: in out Aa: function (type parameter) A in Prism<in out S, in out A>A) => function (type parameter) S in Prism<in out S, in out A>S
}