Hyperlinkv0.8.0-beta.28

Optic

Optic.Lensinterfaceeffect/Optic.ts:144
Lens<S, A>

Focuses on exactly one part A inside a whole S.

When to use

Use when you always have a value to read and need the original S to produce the updated whole, unlike Iso.

Details

  • get(s) always succeeds and returns A.
  • replace(a, s) returns a new S with the focused part replaced.
  • Extends Optional.
  • Composing a Lens with a Prism or Optional produces an Optional.

Example (Focusing on a struct field)

import { Optic } from "effect"

type Person = { readonly name: string; readonly age: number }

const _name = Optic.id<Person>().key("name")

console.log(_name.get({ name: "Alice", age: 30 }))
// Output: "Alice"
Source effect/Optic.ts:1443 lines
export interface Lens<in out S, in out A> extends Optional<S, A> {
  readonly get: (s: S) => A
}
Referenced by 3 symbols