Class: Steep::Interface::Substitution
- Inherits:
-
Object
- Object
- Steep::Interface::Substitution
show all
- Defined in:
- lib/steep/interface/substitution.rb
Defined Under Namespace
Classes: InvalidSubstitutionError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(dictionary:, instance_type:, module_type:, self_type:) ⇒ Substitution
Returns a new instance of Substitution.
21
22
23
24
25
26
|
# File 'lib/steep/interface/substitution.rb', line 21
def initialize(dictionary:, instance_type:, module_type:, self_type:)
@dictionary = dictionary
@instance_type = instance_type
@module_type = module_type
@self_type = self_type
end
|
Instance Attribute Details
#dictionary ⇒ Object
Returns the value of attribute dictionary.
16
17
18
|
# File 'lib/steep/interface/substitution.rb', line 16
def dictionary
@dictionary
end
|
#instance_type ⇒ Object
Returns the value of attribute instance_type.
17
18
19
|
# File 'lib/steep/interface/substitution.rb', line 17
def instance_type
@instance_type
end
|
#module_type ⇒ Object
Returns the value of attribute module_type.
18
19
20
|
# File 'lib/steep/interface/substitution.rb', line 18
def module_type
@module_type
end
|
#self_type ⇒ Object
Returns the value of attribute self_type.
19
20
21
|
# File 'lib/steep/interface/substitution.rb', line 19
def self_type
@self_type
end
|
Class Method Details
.build(vars, types = nil, instance_type: nil, module_type: nil, self_type: nil) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/steep/interface/substitution.rb', line 92
def self.build(vars, types = nil, instance_type: nil, module_type: nil, self_type: nil)
types ||= vars.map {|var| AST::Types::Var.fresh(var) }
raise InvalidSubstitutionError.new(vars_size: vars.size, types_size: types.size) unless vars.size == types.size
dic = vars.zip(types).each.with_object({}) do |(var, type), d| type or raise
d[var] = type
end
new(dictionary: dic, instance_type: instance_type, module_type: module_type, self_type: self_type)
end
|
Instance Method Details
67
68
69
|
# File 'lib/steep/interface/substitution.rb', line 67
def [](key)
dictionary[key] or raise "Unknown variable: #{key}"
end
|
#add!(v, ty) ⇒ Object
154
155
156
|
# File 'lib/steep/interface/substitution.rb', line 154
def add!(v, ty)
merge!(Substitution.new(dictionary: { v => ty }, instance_type: instance_type, module_type: module_type, self_type: self_type))
end
|
#empty? ⇒ Boolean
35
36
37
38
39
40
|
# File 'lib/steep/interface/substitution.rb', line 35
def empty?
dictionary.empty? &&
instance_type.is_a?(AST::Types::Instance) &&
module_type.is_a?(AST::Types::Class) &&
self_type.is_a?(AST::Types::Self)
end
|
#except(vars) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/steep/interface/substitution.rb', line 105
def except(vars)
self.class.new(
dictionary: dictionary.dup,
instance_type: instance_type,
module_type: module_type,
self_type: self_type
).except!(vars)
end
|
#except!(vars) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/steep/interface/substitution.rb', line 114
def except!(vars)
vars.each do |var|
dictionary.delete(var)
end
self
end
|
#key?(var) ⇒ Boolean
71
72
73
|
# File 'lib/steep/interface/substitution.rb', line 71
def key?(var)
dictionary.key?(var)
end
|
147
148
149
150
151
152
|
# File 'lib/steep/interface/substitution.rb', line 147
def merge(s)
Substitution.new(dictionary: dictionary.dup,
instance_type: instance_type,
module_type: module_type,
self_type: self_type).merge!(s)
end
|
#merge!(s, overwrite: false) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/steep/interface/substitution.rb', line 122
def merge!(s, overwrite: false)
dictionary.transform_values! {|ty| ty.subst(s) }
dictionary.merge!(s.dictionary) do |key, a, b|
if a == b
a
else
if overwrite
b
else
raise "Duplicated key on merge!: #{key}, #{a}, #{b} (#{self})"
end
end
end
@instance_type = instance_type.subst(s) if instance_type
@module_type = module_type.subst(s) if module_type
@self_type = self_type.subst(s) if self_type
self
end
|
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/steep/interface/substitution.rb', line 53
def to_s
a = []
dictionary.each do |x, ty|
a << "#{x} -> #{ty}"
end
a << "[instance_type] -> #{instance_type}"
a << "[module_type] -> #{module_type}"
a << "[self_type] -> #{self_type}"
"{ #{a.join(", ")} }"
end
|
#update(self_type: self_type(), instance_type: instance_type(), module_type: module_type()) ⇒ Object
143
144
145
|
# File 'lib/steep/interface/substitution.rb', line 143
def update(self_type: self_type(), instance_type: instance_type(), module_type: module_type())
Substitution.new(dictionary: dictionary.dup, instance_type: instance_type, self_type: self_type, module_type: module_type)
end
|