mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add lazy class
This commit is contained in:
parent
d5cd756882
commit
0af4c2f54f
1 changed files with 34 additions and 0 deletions
34
src/spectator/lazy.cr
Normal file
34
src/spectator/lazy.cr
Normal file
|
@ -0,0 +1,34 @@
|
|||
module Spectator
|
||||
# Lazy initialization of a value.
|
||||
# Constructs a value only once by calling a `Proc`.
|
||||
# The value is then stored and reused - the `Proc` is only called once.
|
||||
struct Lazy(T)
|
||||
@value_or_block : Proc(T) | T
|
||||
|
||||
# Creates a lazy instance.
|
||||
# The block provided to this method will be called
|
||||
# when `#value` is invoked.
|
||||
# The block will only be called once,
|
||||
# and the result of the block will be cached.
|
||||
def initialize(&block : -> T)
|
||||
@value_or_block = block
|
||||
end
|
||||
|
||||
# Retrieves the lazy initialized value.
|
||||
# The first call to this method will create the value.
|
||||
# Subsequent calls will return the same value.
|
||||
def value : T
|
||||
if value = @value_or_block.as?(T)
|
||||
return value
|
||||
else
|
||||
@value_or_block = construct
|
||||
end
|
||||
end
|
||||
|
||||
# Calls the block used to construct the value.
|
||||
# This method can only be called once per instance.
|
||||
private def construct : T
|
||||
@value_or_block.as(Proc(T)).call
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue