Class: RBS::AST::TypeParam

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/ast/type_param.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, variance:, upper_bound:, lower_bound:, location:, default_type: nil, unchecked: false) ⇒ TypeParam

Returns a new instance of TypeParam.



8
9
10
11
12
13
14
15
16
# File 'lib/rbs/ast/type_param.rb', line 8

def initialize(name:, variance:, upper_bound:, lower_bound:, location:, default_type: nil, unchecked: false)
  @name = name
  @variance = variance
  @upper_bound_type = upper_bound
  @lower_bound_type = lower_bound
  @location = location
  @default_type = default_type
  @unchecked = unchecked
end

Instance Attribute Details

#default_typeObject (readonly)

Returns the value of attribute default_type.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def default_type
  @default_type
end

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def location
  @location
end

#lower_bound_typeObject (readonly)

Returns the value of attribute lower_bound_type.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def lower_bound_type
  @lower_bound_type
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def name
  @name
end

#upper_bound_typeObject (readonly)

Returns the value of attribute upper_bound_type.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def upper_bound_type
  @upper_bound_type
end

#varianceObject (readonly)

Returns the value of attribute variance.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def variance
  @variance
end

Class Method Details

.application(params, args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rbs/ast/type_param.rb', line 166

def self.application(params, args)
  if params.empty?
    return nil
  end

  optional_params, required_params = params.partition {|param| param.default_type }

  param_subst = Substitution.new()
  app_subst = Substitution.new()

  required_params.zip(args.take(required_params.size)).each do |param, arg|
    arg ||= Types::Bases::Any.new(location: nil)
    param_subst.add(from: param.name, to: arg)
    app_subst.add(from: param.name, to: arg)
  end

  optional_params.each do |param|
    param_subst.add(from: param.name, to: Types::Bases::Any.new(location: nil))
  end

  optional_params.zip(args.drop(required_params.size)).each do |param, arg|
    if arg
      app_subst.add(from: param.name, to: arg)
    else
      param.default_type or raise
      app_subst.add(from: param.name, to: param.default_type.sub(param_subst))
    end
  end

  app_subst
end

.normalize_args(params, args) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rbs/ast/type_param.rb', line 198

def self.normalize_args(params, args)
  app = application(params, args) or return args

  min_count = params.count { _1.default_type.nil? }
  unless min_count <= args.size && args.size <= params.size
    return args
  end

  params.zip(args).filter_map do |param, arg|
    if arg
      arg
    else
      if param.default_type
        param.default_type.sub(app)
      else
        Types::Bases::Any.new(location: nil)
      end
    end
  end
end

.rename(params, new_names:) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rbs/ast/type_param.rb', line 114

def self.rename(params, new_names:)
  raise unless params.size == new_names.size

  subst = Substitution.build(new_names, Types::Variable.build(new_names))

  params.map.with_index do |param, index|
    new_name = new_names[index]

    TypeParam.new(
      name: new_name,
      variance: param.variance,
      upper_bound: param.upper_bound_type&.map_type {|type| type.sub(subst) },
      lower_bound: param.lower_bound_type&.map_type {|type| type.sub(subst) },
      location: param.location,
      default_type: param.default_type&.map_type {|type| type.sub(subst) }
    ).unchecked!(param.unchecked?)
  end
end

.resolve_variables(params) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/rbs/ast/type_param.rb', line 92

def self.resolve_variables(params)
  return if params.empty?

  vars = Set.new(params.map(&:name))

  params.map! do |param|
    param.map_type {|bound| _ = subst_var(vars, bound) }
  end
end

.subst_var(vars, type) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rbs/ast/type_param.rb', line 102

def self.subst_var(vars, type)
  case type
  when Types::ClassInstance
    namespace = type.name.namespace
    if namespace.relative? && namespace.empty? && vars.member?(type.name.name)
      return Types::Variable.new(name: type.name.name, location: type.location)
    end
  end

  type.map_type {|t| subst_var(vars, t) }
end

.validate(type_params) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/rbs/ast/type_param.rb', line 219

def self.validate(type_params)
  optionals = type_params.filter {|param| param.default_type }

  optional_param_names = optionals.map(&:name).sort

  optionals.filter! do |param|
    default_type = param.default_type or raise
    optional_param_names.any? { default_type.free_variables.include?(_1) }
  end

  unless optionals.empty?
    optionals
  end
end

Instance Method Details

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



41
42
43
44
45
46
47
48
49
# File 'lib/rbs/ast/type_param.rb', line 41

def ==(other)
  other.is_a?(TypeParam) &&
    other.name == name &&
    other.variance == variance &&
    other.upper_bound_type == upper_bound_type &&
    other.lower_bound_type == lower_bound_type &&
    other.default_type == default_type &&
    other.unchecked? == unchecked?
end

#hashObject



53
54
55
# File 'lib/rbs/ast/type_param.rb', line 53

def hash
  self.class.hash ^ name.hash ^ variance.hash ^ upper_bound_type.hash ^ lower_bound_type.hash ^ unchecked?.hash ^ default_type.hash
end

#lower_boundObject



25
26
27
28
29
30
# File 'lib/rbs/ast/type_param.rb', line 25

def lower_bound
  case lower_bound_type
  when Types::ClassInstance, Types::ClassSingleton, Types::Interface
    lower_bound_type
  end
end

#map_type(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rbs/ast/type_param.rb', line 69

def map_type(&block)
  if b = upper_bound_type
    _upper_bound_type = yield(b)
  end

  if b = lower_bound_type
    _lower_bound_type = yield(b)
  end

  if dt = default_type
    _default_type = yield(dt)
  end

  TypeParam.new(
    name: name,
    variance: variance,
    upper_bound: _upper_bound_type,
    lower_bound: _lower_bound_type,
    location: location,
    default_type: _default_type
  ).unchecked!(unchecked?)
end

#to_json(state = JSON::State.new) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbs/ast/type_param.rb', line 57

def to_json(state = JSON::State.new)
  {
    name: name,
    variance: variance,
    unchecked: unchecked?,
    location: location,
    upper_bound: upper_bound_type,
    lower_bound: lower_bound_type,
    default_type: default_type
  }.to_json(state)
end

#to_sObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rbs/ast/type_param.rb', line 133

def to_s
  s = +""

  if unchecked?
    s << "unchecked "
  end

  case variance
  when :invariant
    # nop
  when :covariant
    s << "out "
  when :contravariant
    s << "in "
  end

  s << name.to_s

  if type = upper_bound_type
    s << " < #{type}"
  end

  if type = lower_bound_type
    s << " > #{type}"
  end

  if dt = default_type
    s << " = #{dt}"
  end

  s
end

#unchecked!(value = true) ⇒ Object



32
33
34
35
# File 'lib/rbs/ast/type_param.rb', line 32

def unchecked!(value = true)
  @unchecked = value ? true : false
  self
end

#unchecked?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rbs/ast/type_param.rb', line 37

def unchecked?
  @unchecked
end

#upper_boundObject



18
19
20
21
22
23
# File 'lib/rbs/ast/type_param.rb', line 18

def upper_bound
  case upper_bound_type
  when Types::ClassInstance, Types::ClassSingleton, Types::Interface
    upper_bound_type
  end
end