struct Spectator::Wrapper

Overview

Typeless wrapper for a value. Stores any value or reference type. 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)

Defined in:

spectator/wrapper.cr

Constructors

Instance Method Summary

Instance methods inherited from class Object

should(matcher : Spectator::Matchers::TypeMatcher(U), message = nil, *, _file = __FILE__, _line = __LINE__) forall U
should(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
should
, should_eventually(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) should_eventually, should_never(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) should_never, should_not(matcher : Spectator::Matchers::TypeMatcher(U), message = nil, *, _file = __FILE__, _line = __LINE__) forall U
should_not(matcher : Spectator::Matchers::NilMatcher, message = nil, *, _file = __FILE__, _line = __LINE__)
should_not(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
should_not

Constructor Detail

def self.new(value) #

Creates a wrapper for the specified value.


[View source]

Instance Method Detail

def get(type : T.class) : T forall T #

Retrieves the previously wrapped value. The type of the wrapped value must match otherwise an error will be raised.


[View source]
def get(& : -> T) : T forall T #

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.

wrapper = Wrapper.new(Int32)
# type = wrapper.get(Int32.class) # Does not work!
type = wrapper.get { Int32 } # Returns Int32

[View source]