class Algebrick::ParametrizedType

Attributes

fields[R]
variables[R]
variants[R]

Public Class Methods

new(variables) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 25
def initialize(variables)
  @variables     = variables.each { |v| Type! v, Symbol }
  @fields        = nil
  @variants      = nil
  @cache         = {}
  @cache_barrier = Monitor.new
end

Public Instance Methods

==(other) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 89
def ==(other)
  other.kind_of? ParametrizedType and
      self.generic == other.generic
end
[](*assigned_types) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 49
def [](*assigned_types)
  @cache_barrier.synchronize do
    @cache[assigned_types] || begin
      raise ArgumentError unless assigned_types.size == variables.size
      ProductVariant.new(type_name(assigned_types)).tap do |type|
        type.be_kind_of self
        @cache[assigned_types] = type
        type.assigned_types    = assigned_types
        type.set_variants *insert_types(variants, assigned_types) if variants
        type.set_fields insert_types(fields, assigned_types) if fields
      end
    end
  end
end
call(*field_matchers) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 84
def call(*field_matchers)
  raise TypeError unless @fields
  Matchers::Product.new self, *field_matchers
end
generic() click to toggle source
# File lib/algebrick/parametrized_type.rb, line 64
def generic
  @generic ||= self[*Array(variables.size) { Object }]
end
inspect() click to toggle source
# File lib/algebrick/parametrized_type.rb, line 72
def inspect
  to_s
end
set_fields(fields) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 33
def set_fields(fields)
  @fields      = Type! fields, Hash, Array
  @field_names = case @fields
                 when Hash
                   @fields.keys
                 when Array, nil
                   nil
                 else
                   raise
                 end
end
set_variants(*variants) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 45
def set_variants(*variants)
  @variants = Type! variants, Array
end
to_m() click to toggle source
# File lib/algebrick/parametrized_type.rb, line 76
def to_m
  if @variants
    Matchers::Variant.new self
  else
    Matchers::Product.new self
  end
end
to_s() click to toggle source
# File lib/algebrick/parametrized_type.rb, line 68
def to_s
  "#{name}[#{variables.join(', ')}]"
end

Private Instance Methods

insert_type(type, assigned_types) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 107
def insert_type(type, assigned_types)
  case type
  when Symbol
    assigned_types[variables.index type]
  when ParametrizedType
    type[*type.variables.map { |v| assigned_types[variables.index v] }]
  else
    type
  end
end
insert_types(types, assigned_types) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 96
def insert_types(types, assigned_types)
  case types
  when Hash
    types.inject({}) { |h, (k, v)| h.update k => insert_type(v, assigned_types) }
  when Array
    types.map { |v| insert_type v, assigned_types }
  else
    raise ArgumentError
  end
end
type_name(assigned_types) click to toggle source
# File lib/algebrick/parametrized_type.rb, line 118
def type_name(assigned_types)
  "#{name}[#{assigned_types.join(', ')}]"
end