Class: RaaP::TypeSubstitution

Inherits:
Object
  • Object
show all
Defined in:
lib/raap/type_substitution.rb

Instance Method Summary collapse

Constructor Details

#initialize(type_params, type_args) ⇒ TypeSubstitution



5
6
7
8
# File 'lib/raap/type_substitution.rb', line 5

def initialize(type_params, type_args)
  @type_params = type_params
  @type_args = type_args
end

Instance Method Details

#buildObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/raap/type_substitution.rb', line 10

def build
  bound_map = @type_params.zip(@type_args).to_h do |(bound, arg)|
    if arg
      [bound.name, arg]
    elsif bound.upper_bound
      [bound.name, bound.upper_bound]
    else
      [bound.name, ::RBS::Types::Variable.new(name: bound.name, location: nil)]
    end
  end
  ::RBS::Substitution.build(bound_map.keys, bound_map.values)
end

#method_type_sub(method_type, self_type: nil, instance_type: nil, class_type: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/raap/type_substitution.rb', line 23

def method_type_sub(method_type, self_type: nil, instance_type: nil, class_type: nil)
  self_type = self_type.is_a?(::String) ? RBS.parse_type(self_type) : self_type
  instance_type = instance_type.is_a?(::String) ? RBS.parse_type(instance_type) : instance_type
  class_type = class_type.is_a?(::String) ? RBS.parse_type(class_type) : class_type
  sub = build
  if sub.empty? && self_type.nil? && instance_type.nil? && class_type.nil?
    return method_type
  end

  ::RBS::MethodType.new(
    type_params: [],
    type: method_type.type.sub(sub).then { |ty| sub(ty, self_type: self_type, instance_type: instance_type, class_type: class_type) },
    block: method_type.block&.sub(sub)&.then { |bl| sub(bl, self_type: self_type, instance_type: instance_type, class_type: class_type) },
    location: method_type.location
  )
end