Class: Algebrick::ParametrizedType

Inherits:
Type show all
Includes:
FieldMethodReaders, MatcherDelegations, TypeCheck
Defined in:
lib/algebrick/parametrized_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldMethodReaders

#add_all_field_method_readers, #add_field_method_reader, #add_field_method_readers, #field_names, #field_names?

Methods included from MatcherDelegations

#!, #&, #>, #>>, #case, #|, #~

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?

Methods inherited from Type

#be_kind_of, #match, #name

Methods included from Reclude

#include, #included

Methods included from Matching

#any, #match, #match?, #on

Constructor Details

#initialize(variables) ⇒ ParametrizedType

Returns a new instance of ParametrizedType.



25
26
27
28
29
30
31
# 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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



23
24
25
# File 'lib/algebrick/parametrized_type.rb', line 23

def fields
  @fields
end

#variablesObject (readonly)

Returns the value of attribute variables.



23
24
25
# File 'lib/algebrick/parametrized_type.rb', line 23

def variables
  @variables
end

#variantsObject (readonly)

Returns the value of attribute variants.



23
24
25
# File 'lib/algebrick/parametrized_type.rb', line 23

def variants
  @variants
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
92
# File 'lib/algebrick/parametrized_type.rb', line 89

def ==(other)
  other.kind_of? ParametrizedType and
      self.generic == other.generic
end

#[](*assigned_types) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

Raises:

  • (TypeError)


84
85
86
87
# File 'lib/algebrick/parametrized_type.rb', line 84

def call(*field_matchers)
  raise TypeError unless @fields
  Matchers::Product.new self, *field_matchers
end

#genericObject



64
65
66
# File 'lib/algebrick/parametrized_type.rb', line 64

def generic
  @generic ||= self[*Array(variables.size) { Object }]
end

#inspectObject



72
73
74
# File 'lib/algebrick/parametrized_type.rb', line 72

def inspect
  to_s
end

#set_fields(fields) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



45
46
47
# File 'lib/algebrick/parametrized_type.rb', line 45

def set_variants(*variants)
  @variants = Type! variants, Array
end

#to_mObject



76
77
78
79
80
81
82
# 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_sObject



68
69
70
# File 'lib/algebrick/parametrized_type.rb', line 68

def to_s
  "#{name}[#{variables.join(', ')}]"
end