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.

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.

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.

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.

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) ```

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) ```

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.