Class: RDL::Type::TupleType
- Inherits:
-
Type
- Object
- Type
- RDL::Type::TupleType
show all
- Defined in:
- lib/rdl/types/tuple.rb
Overview
A specialized GenericType for tuples, i.e., fixed-sized arrays
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Type
leq, #nil_type?, #optional?, #to_contract, #vararg?
Constructor Details
#initialize(*params) ⇒ TupleType
no caching because array might be mutated
10
11
12
13
14
15
16
17
18
|
# File 'lib/rdl/types/tuple.rb', line 10
def initialize(*params)
raise RuntimeError, "Attempt to create tuple type with non-type param" unless params.all? { |p| p.is_a? Type }
@params = params
@array = nil @cant_promote = false
@ubounds = []
@lbounds = []
super()
end
|
Instance Attribute Details
#array ⇒ Object
either nil or array type if self has been promoted to array
5
6
7
|
# File 'lib/rdl/types/tuple.rb', line 5
def array
@array
end
|
#lbounds ⇒ Object
7
8
9
|
# File 'lib/rdl/types/tuple.rb', line 7
def lbounds
@lbounds
end
|
#params ⇒ Object
Returns the value of attribute params.
4
5
6
|
# File 'lib/rdl/types/tuple.rb', line 4
def params
@params
end
|
#ubounds ⇒ Object
upper bounds this tuple has been compared with using <=
6
7
8
|
# File 'lib/rdl/types/tuple.rb', line 6
def ubounds
@ubounds
end
|
Instance Method Details
#<=(other) ⇒ Object
60
61
62
|
# File 'lib/rdl/types/tuple.rb', line 60
def <=(other)
return Type.leq(self, other)
end
|
#==(other) ⇒ Object
Also known as:
eql?
30
31
32
33
34
35
|
# File 'lib/rdl/types/tuple.rb', line 30
def ==(other) return false if other.nil?
return (@array == other) if @array
other = other.canonical
return (other.instance_of? TupleType) && (other.params == @params)
end
|
#canonical ⇒ Object
20
21
22
23
|
# File 'lib/rdl/types/tuple.rb', line 20
def canonical
return @array if @array
return self
end
|
55
56
57
58
|
# File 'lib/rdl/types/tuple.rb', line 55
def cant_promote!
raise RuntimeError, "already promoted!" if @array
@cant_promote = true
end
|
#hash ⇒ Object
77
78
79
80
|
# File 'lib/rdl/types/tuple.rb', line 77
def hash
73 * @params.hash
end
|
#instantiate(inst) ⇒ Object
72
73
74
75
|
# File 'lib/rdl/types/tuple.rb', line 72
def instantiate(inst)
return @array.instantiate(inst) if @array
return TupleType.new(*@params.map { |t| t.instantiate(inst) })
end
|
#match(other) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/rdl/types/tuple.rb', line 39
def match(other)
return @array.match(other) if @array
other = other.canonical
other = other.type if other.instance_of? AnnotatedArgType
return true if other.instance_of? WildQuery
return (other.instance_of? TupleType) && (@params.length == other.params.length) && (@params.zip(other.params).all? { |t,o| t.match(o) })
end
|
#member?(obj, *args) ⇒ Boolean
64
65
66
67
68
69
70
|
# File 'lib/rdl/types/tuple.rb', line 64
def member?(obj, *args)
return @array.member?(obj, *args) if @array
t = RDL::Util.rdl_type obj
return t <= self if t
return false unless obj.instance_of?(Array) && obj.size == @params.size
return @params.zip(obj).all? { |formal, actual| formal.member?(actual, *args) }
end
|
47
48
49
50
51
52
53
|
# File 'lib/rdl/types/tuple.rb', line 47
def promote!
return false if @cant_promote
@array = GenericType.new(RDL::Globals.types[:array], UnionType.new(*@params))
return (@lbounds.all? { |lbound| lbound <= self }) && (@ubounds.all? { |ubound| self <= ubound })
end
|
#to_s ⇒ Object
25
26
27
28
|
# File 'lib/rdl/types/tuple.rb', line 25
def to_s
return @array.to_s if @array
return "[#{@params.map { |t| t.to_s }.join(', ')}]"
end
|