Wrapper(T)
Wraps an object such that methods are delegated to the wrapped object. The return values of the methods will be wrapped as well if the return type is the same as the wrapped object.
struct SpecialArray
include Wrapper(Array(Int32))
fully_delegate
end
arr = [1, 2, 3, 4]
sarr = SpecialArray.new arr
sarr[0] # => 1
sarr[-1] # => 4
# Returned objects are wrapped if appropiate
sarr[0..2] # => SpecialArray[1, 2, 3]
sarr.select(&.even?) # => SpecialArray[2, 4]
# Write access the original object
sarr.select!(&.odd?)
sarr # => SpecialArray[1, 3]
arr # => [1, 3]
sarr.wrapped.same? arr # => true
Constructors
new(wrapped : T = T.new)
SourceInstance methods
==(rhs : T) : Bool
Source==(rhs : self) : Bool
Sourcepretty_print(*args, **options)
Sourcepretty_print(*args, **options, &)
Sourceto_s(*args, **options)
Sourceto_s(*args, **options, &)
SourceMacros
delegate(*methods)
Delegates methods to the enclosed object, wrapping the return value if it is of the same type as the enclosed object.
struct CustomInt
include Wrapper(Int32)
delegate :+
end
val = CustomInt.new(5) + 10
val.class # => CustomInt
val # => 15