struct Spectator::LazyWrapper

Overview

Lazily stores a value of any type. Combines Lazy and Wrapper.

Contains no value until the first call to #get is made. Any type can be stored in this wrapper. However, the type must always be known when retrieving it via #get. The type is inferred from the block, and all blocks must return the same type. Because of this, it is recommended to only have #get called in one location.

This type is expected to be used like so:

@wrapper : LazyWrapper

# ...

def lazy_load
  @wrapper.get { some_expensive_operation }
end

Defined in:

spectator/lazy_wrapper.cr

Constructors

Instance Method Summary

Instance methods inherited from class Object

should(matcher, message = nil) should, should_eventually(matcher, message = nil) should_eventually, should_never(matcher, message = nil) should_never, should_not(matcher, message = nil) should_not

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def get(& : -> T) : T forall T #

Retrieves the value, if it was previously fetched. On the first invocation of this method, it will yield. The block should return the value to store. Subsequent calls will return the same value and not yield.


[View source]