Workaround for case where wrapper might store a type

This commit is contained in:
Michael Miller 2021-01-30 23:49:20 -07:00
parent e275711f2b
commit a20b7cad80
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
2 changed files with 11 additions and 1 deletions

View file

@ -13,7 +13,7 @@ module Spectator
# 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(&block : -> T) : T forall T
wrapper = @lazy.get { Wrapper.new(yield) } wrapper = @lazy.get { Wrapper.new(yield) }
wrapper.get(T) wrapper.get { yield }
end end
end end
end end

View file

@ -17,6 +17,16 @@ module Spectator
value.get value.get
end end
# Retrieves the previously wrapped value.
# Alternate form of `#get` that accepts a block.
# 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`).
# The block will never be executed, it is only used for type information.
def get(& : -> T) : T forall T
value = @value.as(Value(T))
value.get
end
# Base type that generic types inherit from. # Base type that generic types inherit from.
# This provides a common base type, # This provides a common base type,
# since Crystal doesn't support storing an `Object` (yet). # since Crystal doesn't support storing an `Object` (yet).