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.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(wrapped : T = T.new) #

[View source]

Instance Method Detail

def ==(rhs : T) : Bool #

[View source]
def ==(rhs : self) : Bool #

[View source]
def pretty_print(*args, **options) #

[View source]
def pretty_print(*args, **options, &) #

[View source]
def to_s(*args, **options) #

[View source]
def to_s(*args, **options, &) #

[View source]
def wrapped : T #

Returns the wrapped object.


[View source]

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

[View source]
macro fully_delegate #

Delegates missing methods to the wrapped object using the method_missing hook.


[View source]