module Wrapper(T)
Overview
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
Direct including types
Defined in:
views/wrapper.crConstructors
Instance Method Summary
- #==(rhs : T) : Bool
- #==(rhs : self) : Bool
- #pretty_print(*args, **options)
- #pretty_print(*args, **options, &)
- #to_s(*args, **options)
- #to_s(*args, **options, &)
-
#wrapped : T
Returns the wrapped object.
Macro Summary
-
delegate(*methods)
Delegates methods to the enclosed object, wrapping the return value if it is of the same type as the enclosed object.
-
fully_delegate
Delegates missing methods to the wrapped object using the
method_missing
hook.
Constructor Detail
Instance Method Detail
Macro Detail
macro 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
macro fully_delegate
#
Delegates missing methods to the wrapped object using the
method_missing
hook.