DocsPlaygroundBlogCommunityPackages
  • Playground
  • Blog
  • Community
  • Packages
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
Overview
Stdlib
submodules
  • Array
  • ArrayBuffer
  • AsyncIterator
  • BigInt
  • BigInt64Array
    • Constants
    BigUint64Array
    • Constants
  • Bool
  • Console
  • DataView
  • Date
    • UTC
  • Dict
  • Error
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • Exn
  • Float
    • Constants
    Float32Array
    • Constants
    Float64Array
    • Constants
    Int
    • Ref
    • Bitwise
    • Constants
    Int16Array
    • Constants
    Int32Array
    • Constants
    Int8Array
    • Constants
  • IntervalId
  • Intl
    • Segments
    • Segmenter
    • RelativeTimeFormat
    • PluralRules
    • NumberFormat
      • Grouping
    • Locale
    • ListFormat
    • DateTimeFormat
    • Collator
    • Common
  • Iterator
  • JSON
    • Decode
    • Encode
    • Classify
    JsError
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • JsExn
  • Lazy
    • t
      t
    • v
      make
    • v
      get
    • v
      isEvaluated
    • v
      force
      D
    • v
      force_val
      D
    • v
      from_fun
      D
    • v
      from_val
      D
    • v
      is_val
      D
  • List
  • Map
  • Math
    • Int
    • Constants
  • Null
  • Nullable
  • Object
  • Option
  • Ordering
  • Pair
  • Promise
  • RegExp
    • Result
  • Result
  • Set
  • String
  • Symbol
  • TimeoutId
  • Type
    • Classify
  • TypedArray
  • Uint16Array
    • Constants
    Uint32Array
    • Constants
    Uint8Array
    • Constants
    Uint8ClampedArray
    • Constants
  • WeakMap
  • WeakSet
  • Docs / API / Stdlib / Lazy

    Lazy

    This module provides a type Lazy.t and functions to create and manipulate lazy values. A lazy value is a value that is not computed until it is needed. This is useful for deferring computations that may be expensive or unnecessary.

    t

    RESCRIPT
    type t<+'a>

    The type of a lazy value. Lazy.t<'a> represents a lazy value that will eventually yield a value of type 'a when accessed. The value is computed only once, and the result is cached for subsequent accesses. If the computation throws an exception, the same exception is thrown again on subsequent accesses.

    make

    RESCRIPT
    let make: (unit => 'a) => t<'a>

    Lazy.make(f) creates a lazy value from f which is the computation to be deferred of type unit => 'a. The function returns a lazy value of type Lazy.t<'a>.
    The computation is not executed until the lazy value is accessed.

    ## Examples ```rescript let lazyValue = Lazy.make(() => { // Some expensive computation Console.log("Computing...") 42 }); lazyValue->Lazy.get->assertEqual(42) ```

    get

    RESCRIPT
    let get: t<'a> => 'a

    Lazy.get(x) forces the suspension x and returns its result. If x has already been forced, Lazy.get(x) returns the same value again without recomputing it. If it threw an exception, the same exception is thrown again. Throw Undefined if the forcing of x tries to force x itself recursively. This is a runtime error.

    isEvaluated

    RESCRIPT
    let isEvaluated: t<'a> => bool

    Lazy.isEvaluated(x) returns true if the suspension x has already been forced and did not throw an exception. Otherwise, it returns false. This is useful for checking if a lazy value has been computed before accessing it.

    ## Examples ```rescript let lazyValue = Lazy.make(() => { // Some expensive computation Console.log("Computing...") 42 }) Lazy.isEvaluated(lazyValue)->assertEqual(false) lazyValue->Lazy.get->assertEqual(42) lazyValue->Lazy.isEvaluated->assertEqual(true) ```

    force

    Deprecated

    RESCRIPT
    let force: t<'a> => 'a

    force(x) forces the suspension x and returns its result. If x has already been forced, Lazy.force(x) returns the same value again without recomputing it. If it threw an exception, the same exception is thrown again. Throw Undefined if the forcing of x tries to force x itself recursively.

    force_val

    Deprecated

    RESCRIPT
    let force_val: t<'a> => 'a

    force_val(x) forces the suspension x and returns its result. If x has already been forced, force_val(x) returns the same value again without recomputing it. Throw Undefined if the forcing of x tries to force x itself recursively. If the computation of x throws an exception, it is unspecified whether force_val(x) throws the same exception or Undefined.

    from_fun

    Deprecated

    RESCRIPT
    let from_fun: (unit => 'a) => t<'a>

    Lazy.from_fun(f) creates a lazy value from f which is the computation to be deferred of type unit => 'a. The function returns a lazy value of type Lazy.t<'a>.
    The computation is not executed until the lazy value is accessed.

    from_val

    Deprecated

    RESCRIPT
    let from_val: 'a => t<'a>

    from_val(v) returns an already-forced suspension of v. This is for special purposes only.

    is_val

    Deprecated

    RESCRIPT
    let is_val: t<'a> => bool

    is_val(x) returns true if `x has already been forced and did not throw an exception.

    Types and values
    • t
      t
    • v
      make
    • v
      get
    • v
      isEvaluated
    • v
      force
      D
    • v
      force_val
      D
    • v
      from_fun
      D
    • v
      from_val
      D
    • v
      is_val
      D

    © 2025 The ReScript Project

    About
    • Community
    • ReScript Association
    Find us on