happy-rusty
    Preparing search index...

    Interface Lazy<T>

    A value which is initialized on the first access.

    The initialization function is provided at construction time and executed on first access. Subsequent accesses return the cached value.

    1.6.0

    const expensive = Lazy(() => {
    console.log('Computing...');
    return heavyComputation();
    });

    // Nothing computed yet
    console.log(expensive.isInitialized()); // false

    // First access triggers computation
    const value = expensive.force(); // logs "Computing..."

    // Subsequent access returns cached value
    const same = expensive.force(); // no log, returns cached value
    interface Lazy<T> {
        "[toStringTag]": "Lazy";
        force(): T;
        get(): Option<T>;
        isInitialized(): boolean;
        toString(): string;
    }

    Type Parameters

    • T

      The type of the value stored.

    Index

    Properties

    "[toStringTag]": "Lazy"

    The well-known symbol Symbol.toStringTag used by Object.prototype.toString(). Returns 'Lazy' so that Object.prototype.toString.call(lazy) produces '[object Lazy]'.

    Methods

    • Forces the evaluation of this lazy value and returns the result.

      If the value has already been initialized, returns the cached value. Otherwise, executes the initialization function, caches the result, and returns it.

      Returns T

      The initialized value.

      Rethrows any exception thrown by the initialization function.

      const lazy = Lazy(() => 42);
      console.log(lazy.force()); // 42
      console.log(lazy.force()); // 42 (cached)
    • Gets the value if it has been initialized.

      Unlike force(), this does not trigger initialization.

      Returns Option<T>

      Some(value) if initialized, None otherwise.

      const lazy = Lazy(() => 42);
      console.log(lazy.get()); // None

      lazy.force();
      console.log(lazy.get()); // Some(42)
    • Returns true if the value has been initialized.

      Returns boolean

      const lazy = Lazy(() => 42);
      console.log(lazy.isInitialized()); // false

      lazy.force();
      console.log(lazy.isInitialized()); // true
    • Custom toString implementation.

      Returns string

      const lazy = Lazy(() => 42);
      console.log(lazy.toString()); // 'Lazy(<uninitialized>)'

      lazy.force();
      console.log(lazy.toString()); // 'Lazy(42)'