Class: RDL::Type::GenericType
- Inherits:
-
Type
- Object
- Type
- RDL::Type::GenericType
show all
- Defined in:
- lib/rdl/types/generic.rb
Overview
A type that is parameterized on one or more other types. The base type must be a NominalType or self, while the parameters should be strings or symbols
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Type
#canonical, leq, #nil_type?, #optional?, #to_contract, #vararg?
Constructor Details
#initialize(base, *params) ⇒ GenericType
Returns a new instance of GenericType.
10
11
12
13
14
15
16
|
# File 'lib/rdl/types/generic.rb', line 10
def initialize(base, *params)
raise RuntimeError, "Attempt to create generic type with non-type param" unless params.all? { |p| p.is_a? Type }
raise "base must be NominalType or self, got #{base} of type #{base.class}" unless ((base.instance_of? NominalType) || ((base.instance_of? VarType) && (base.name.to_s == "self")))
@base = base
@params = params
super()
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
7
8
9
|
# File 'lib/rdl/types/generic.rb', line 7
def base
@base
end
|
#params ⇒ Object
Returns the value of attribute params.
8
9
10
|
# File 'lib/rdl/types/generic.rb', line 8
def params
@params
end
|
Instance Method Details
#<=(other) ⇒ Object
38
39
40
|
# File 'lib/rdl/types/generic.rb', line 38
def <=(other)
return Type.leq(self, other)
end
|
#==(other) ⇒ Object
Also known as:
eql?
22
23
24
25
26
|
# File 'lib/rdl/types/generic.rb', line 22
def ==(other) return false if other.nil?
other = other.canonical
return (other.instance_of? GenericType) && (other.base == @base) && (other.params == @params)
end
|
#hash ⇒ Object
55
56
57
|
# File 'lib/rdl/types/generic.rb', line 55
def hash
(61 + @base.hash) * @params.hash
end
|
#instantiate(inst) ⇒ Object
51
52
53
|
# File 'lib/rdl/types/generic.rb', line 51
def instantiate(inst)
GenericType.new(base.instantiate(inst), *params.map { |t| t.instantiate(inst) })
end
|
#match(other) ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/rdl/types/generic.rb', line 30
def match(other)
other = other.canonical
other = other.type if other.instance_of? AnnotatedArgType
return true if other.instance_of? WildQuery
return false unless other.instance_of? GenericType
return @params.length == other.params.length && @params.zip(other.params).all? { |t,o| t.match(o) }
end
|
#member?(obj, *args) ⇒ Boolean
42
43
44
45
46
47
48
49
|
# File 'lib/rdl/types/generic.rb', line 42
def member?(obj, *args)
raise "No type parameters defined for #{base.name}" unless RDL::Globals.type_params[base.name]
t = RDL::Util.rdl_type obj
return t <= self if t
return false unless base.member?(obj, *args)
return true
end
|
#to_inst ⇒ Object
59
60
61
|
# File 'lib/rdl/types/generic.rb', line 59
def to_inst
return RDL::Globals.type_params[base.name][0].zip(@params).to_h
end
|
#to_s ⇒ Object
18
19
20
|
# File 'lib/rdl/types/generic.rb', line 18
def to_s
"#{@base}<#{@params.map { |t| t.to_s }.join(', ')}>"
end
|