2021-01-09 23:51:37 +00:00
|
|
|
require "./lazy"
|
|
|
|
require "./wrapper"
|
|
|
|
|
|
|
|
module Spectator
|
|
|
|
# Lazily stores a value of any type.
|
|
|
|
# Combines `Lazy` and `Wrapper`.
|
|
|
|
struct LazyWrapper
|
|
|
|
@lazy = Lazy(Wrapper).new
|
|
|
|
|
|
|
|
# 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.
|
2021-01-09 23:57:23 +00:00
|
|
|
def get(&block : -> T) : T forall T
|
2021-01-09 23:51:37 +00:00
|
|
|
wrapper = @lazy.get { Wrapper.new(yield) }
|
2021-01-31 06:49:20 +00:00
|
|
|
wrapper.get { yield }
|
2021-01-09 23:51:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|