Package: effect
Module: Optic
Creates a Prism from a fallible getter and an infallible setter.
When to use
Use when reading can fail (the part may not exist in S), but building S
from A always succeeds.
Details
getResult should return Result.fail(message) on mismatch.Example (Parsing a string to a number)
import { Optic, Result } from "effect"
const numeric = Optic.makePrism<string, number>(
(s) => {
const n = Number(s)
return Number.isNaN(n) ? Result.fail("not a number") : Result.succeed(n)
},
String
)
console.log(Result.isSuccess(numeric.getResult("42")))
// Output: true
console.log(numeric.set(42))
// Output: "42"
See
Prism — the type this function returnsfromChecks — build from Schema checks insteadSignature
declare const makePrism: <S, A>(getResult: (s: S) => Result.Result<A, string>, set: (a: A) => S) => Prism<S, A>
Since v4.0.0