mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
More docs
This commit is contained in:
parent
8232da1167
commit
8b5fd099bb
2 changed files with 30 additions and 1 deletions
|
@ -4,6 +4,23 @@ require "./wrapper"
|
||||||
module Spectator
|
module Spectator
|
||||||
# Lazily stores a value of any type.
|
# Lazily stores a value of any type.
|
||||||
# Combines `Lazy` and `Wrapper`.
|
# 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
|
||||||
|
# ```
|
||||||
struct LazyWrapper
|
struct LazyWrapper
|
||||||
@lazy = Lazy(Wrapper).new
|
@lazy = Lazy(Wrapper).new
|
||||||
|
|
||||||
|
@ -11,7 +28,7 @@ module Spectator
|
||||||
# On the first invocation of this method, it will yield.
|
# On the first invocation of this method, it will yield.
|
||||||
# The block should return the value to store.
|
# The block should return the value to store.
|
||||||
# Subsequent calls will return the same value and not yield.
|
# Subsequent calls will return the same value and not yield.
|
||||||
def get(&block : -> T) : T forall T
|
def get(& : -> T) : T forall T
|
||||||
wrapper = @lazy.get { Wrapper.new(yield) }
|
wrapper = @lazy.get { Wrapper.new(yield) }
|
||||||
wrapper.get { yield }
|
wrapper.get { yield }
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,12 @@ module Spectator
|
||||||
# Typeless wrapper for a value.
|
# Typeless wrapper for a value.
|
||||||
# Stores any value or reference type.
|
# Stores any value or reference type.
|
||||||
# However, the type must be known when retrieving the value.
|
# However, the type must be known when retrieving the value.
|
||||||
|
#
|
||||||
|
# This type is expected to be used like so:
|
||||||
|
# ```
|
||||||
|
# wrapper = Wrapper.new("wrapped")
|
||||||
|
# value = wrapper.get(String)
|
||||||
|
# ```
|
||||||
struct Wrapper
|
struct Wrapper
|
||||||
@value : TypelessValue
|
@value : TypelessValue
|
||||||
|
|
||||||
|
@ -22,6 +28,12 @@ module Spectator
|
||||||
# The block must return the same type as the wrapped value, otherwise an error will be raised.
|
# The block must return the same type as the wrapped value, otherwise an error will be raised.
|
||||||
# This method gets around the issue where the value might be a type (i.e. `Int32.class`).
|
# This method gets around the issue where the value might be a type (i.e. `Int32.class`).
|
||||||
# The block will never be executed, it is only used for type information.
|
# The block will never be executed, it is only used for type information.
|
||||||
|
#
|
||||||
|
# ```
|
||||||
|
# wrapper = Wrapper.new(Int32)
|
||||||
|
# # type = wrapper.get(Int32.class) # Does not work!
|
||||||
|
# type = wrapper.get { Int32 } # Returns Int32
|
||||||
|
# ```
|
||||||
def get(& : -> T) : T forall T
|
def get(& : -> T) : T forall T
|
||||||
value = @value.as(Value(T))
|
value = @value.as(Value(T))
|
||||||
value.get
|
value.get
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue