From a20b7cad804d6212464f93ae64bf8942075b9a37 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 30 Jan 2021 23:49:20 -0700 Subject: [PATCH] Workaround for case where wrapper might store a type --- src/spectator/lazy_wrapper.cr | 2 +- src/spectator/wrapper.cr | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/spectator/lazy_wrapper.cr b/src/spectator/lazy_wrapper.cr index ea177f9..3939c95 100644 --- a/src/spectator/lazy_wrapper.cr +++ b/src/spectator/lazy_wrapper.cr @@ -13,7 +13,7 @@ module Spectator # Subsequent calls will return the same value and not yield. def get(&block : -> T) : T forall T wrapper = @lazy.get { Wrapper.new(yield) } - wrapper.get(T) + wrapper.get { yield } end end end diff --git a/src/spectator/wrapper.cr b/src/spectator/wrapper.cr index efee1b6..b5edeb3 100644 --- a/src/spectator/wrapper.cr +++ b/src/spectator/wrapper.cr @@ -17,6 +17,16 @@ module Spectator value.get 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. # This provides a common base type, # since Crystal doesn't support storing an `Object` (yet).