Class: Farce::LazyRef

Inherits:
Reference
  • Object
show all
Defined in:
lib/farce/lazy_ref.rb

Overview

A Reference for a Lazy value.

Results use the lazy value's transfer mode, which defaults to :copy.

Calling freeze resolves and freezes the target. Checking frozen? before the first successful freeze does not resolve it.

Examples:

initialized = Farce::Counter.new

lazy_ref = Farce::LazyRef.new do
  initialized.increment
  Farce::Map.new
end

initialized.value # => 0
lazy_ref[:key] = :value
initialized.value # => 1

Instance Method Summary collapse

Constructor Details

#initialize(factory, mode: :copy) ⇒ LazyRef #initialize(self: nil, mode: :copy) { ... } ⇒ LazyRef

Valid modes are:

  • :copy - The value will be copied between Ractors. This is the default mode.
  • :make_shareable - The value will be made Ractor-shareable using Ractor.make_shareable.
  • :move - The value will be moved between Ractors. This saves memory compared to copying, and supports values that can't be copied but moved (like IO objects). However, the value will no longer be accessible on the Ractor that pushed it.
  • :mutable - A Mutable instance will be created for the value. This isn't done recursively and thus will fail for nested unshareable values.
  • :local - The value will be kept local to the Ractor that pushed it. Another ractor trying to receive it will get an error. Useful for usage contained within a single Ractor.
  • :proxy - The value will be wrapped in a Farce::Proxy that executes calls in the original Ractor.
  • :raise - An error will be raised if the value is not Ractor-shareable. Useful for enforcing shareability.
  • :dedup - The value will be deduplicated using Farce.dedup, then made Ractor-shareable. This may update and freeze the original. Already-shareable values pass through unchanged.
  • :shareable_copy - The value will be copied and the copy will be made Ractor-shareable.

Overloads:

  • #initialize(factory, mode: :copy) ⇒ LazyRef

    Parameters:

    • mode (Symbol) (defaults to: :copy) —

      the transfer mode for the computed result

    • factory (Class, Proc, #call) —

      The factory to use for creating the value. Must be ractor-shareable.

  • #initialize(self: nil, mode: :copy) { ... } ⇒ LazyRef

    Parameters:

    • mode (Symbol) (defaults to: :copy) —

      the transfer mode for the computed result

    • self (BasicObject) (defaults to: nil) —

      The self parameter to be provided to the block. Must be ractor-shareable.

    Yields:

    • The block to use for creating the value. Must be ractor-shareable.

    Yield Returns:

    • (BasicObject) —

      The value to be returned by the lazy instance.

    Yield Receiver:

    • (BasicObject) —

      The self parameter provided, or nil if not provided.



39
# File 'lib/farce/lazy_ref.rb', line 39

LazyRef = Reference[Lazy]