Class: Finitio::StructType

Inherits:
Type
  • Object
show all
Defined in:
lib/finitio/type/struct_type.rb

Overview

The Struct type generator allows capturing positional sequences of values, such as pairs, triples and so forth. For instance, a Point type could be defined as follows:

Point = <Length, Angle>

This class allows capturing those information types, as in:

Length = BuiltinType.new(Fixnum)
Angle  = BuiltinType.new(Float)
Point  = StructType.new([Length, Angle])

A simple Array is used as concrete ruby representation for structs. The values map to the concrete representations of each component type:

R(Point) = Array[R(Length) ^ R(Angle)]
         = Array[Fixnum ^ Float]
         = Array[Numeric]

where ‘^` denotes the `least common super type` operator on ruby classes.

Accordingly, the ‘dress` transformation function has the signature below. It expects it’s Alpha/Object argument to be an Array with all and only the expected components. The ‘dress` function applies on every component according to its type.

dress :: Alpha  -> Point          throws TypeError
dress :: Object -> Array[Numeric] throws TypeError

Constant Summary

Constants included from Metadata

Metadata::EMPTY_METADATA

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#anonymous?, #name, #name=, #named?, #suppremum, #to_s

Methods included from Metadata

#metadata, #metadata=, #metadata?

Constructor Details

#initialize(component_types, name = nil, metadata = nil) ⇒ StructType

Returns a new instance of StructType.



34
35
36
37
38
39
40
41
42
# File 'lib/finitio/type/struct_type.rb', line 34

def initialize(component_types, name = nil,  = nil)
  unless component_types.is_a?(Array) &&
         component_types.all?{|c| c.is_a?(Type) }
    raise ArgumentError, "[Finitio::Type] expected, got `#{component_types}`"
  end

  super(name, )
  @component_types = component_types
end

Instance Attribute Details

#component_typesObject (readonly)

Returns the value of attribute component_types.



43
44
45
# File 'lib/finitio/type/struct_type.rb', line 43

def component_types
  @component_types
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



74
75
76
# File 'lib/finitio/type/struct_type.rb', line 74

def ==(other)
  super || (other.is_a?(StructType) && other.component_types == component_types)
end

#default_nameObject



49
50
51
# File 'lib/finitio/type/struct_type.rb', line 49

def default_name
  "<" + @component_types.map(&:name).join(', ') + ">"
end

#dress(value, handler = DressHelper.new) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/finitio/type/struct_type.rb', line 59

def dress(value, handler = DressHelper.new)
  handler.failed!(self, value) unless value.is_a?(Array)

  # check the size
  cs, vs = component_types.size, value.size
  handler.fail!("Struct size mismatch (#{vs} for #{cs})") unless cs==vs

  # dress components
  array = []
  handler.iterate(value) do |elm, index|
    array << component_types[index].dress(elm, handler)
  end
  array
end

#hashObject



79
80
81
# File 'lib/finitio/type/struct_type.rb', line 79

def hash
  self.class.hash ^ component_types.hash
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/finitio/type/struct_type.rb', line 53

def include?(value)
  value.is_a?(Array) &&
  value.size==component_types.size &&
  value.zip(component_types).all?{|v,t| t.include?(v) }
end

#representatorObject



45
46
47
# File 'lib/finitio/type/struct_type.rb', line 45

def representator
  component_types.map(&:representator)
end

#resolve_proxies(system) ⇒ Object



83
84
85
# File 'lib/finitio/type/struct_type.rb', line 83

def resolve_proxies(system)
  StructType.new(component_types.map{|t| t.resolve_proxies(system)}, name, )
end

#unconstrainedObject



87
88
89
# File 'lib/finitio/type/struct_type.rb', line 87

def unconstrained
  StructType.new(component_types.map{|t| t.unconstrained}, name, )
end