Class: RBS::MethodType

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_params:, type:, block:, location:) ⇒ MethodType

Returns a new instance of MethodType.



10
11
12
13
14
15
# File 'lib/rbs/method_type.rb', line 10

def initialize(type_params:, type:, block:, location:)
  @type_params = type_params
  @type = type
  @block = block
  @location = location
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/rbs/method_type.rb', line 7

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



8
9
10
# File 'lib/rbs/method_type.rb', line 8

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



5
6
7
# File 'lib/rbs/method_type.rb', line 5

def type_params
  @type_params
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
20
21
22
# File 'lib/rbs/method_type.rb', line 17

def ==(other)
  other.is_a?(MethodType) &&
    other.type_params == type_params &&
    other.type == type &&
    other.block == block
end

#each_type(&block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/rbs/method_type.rb', line 84

def each_type(&block)
  if block
    type.each_type(&block)
    self.block&.yield_self do |b|
      b.type.each_type(&block)
    end
  else
    enum_for :each_type
  end
end

#free_variables(set = Set.new) ⇒ Object



57
58
59
60
61
# File 'lib/rbs/method_type.rb', line 57

def free_variables(set = Set.new)
  type.free_variables(set)
  block&.type&.free_variables(set)
  set.subtract(type_param_names)
end

#map_type(&block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rbs/method_type.rb', line 63

def map_type(&block)
  self.class.new(
    type_params: type_params,
    type: type.map_type(&block),
    block: self.block&.map_type(&block),
    location: location
  )
end

#map_type_bound(&block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbs/method_type.rb', line 72

def map_type_bound(&block)
  if type_params.empty?
    self
  else
    self.update(
      type_params: type_params.map {|param|
        param.map_type(&block)
      }
    )
  end
end

#sub(s) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rbs/method_type.rb', line 33

def sub(s)
  sub = s.without(*type_param_names)

  self.class.new(
    type_params: type_params.map do |param|
      param.map_type do |bound|
        bound.map_type {|ty| ty.sub(sub) }
      end
    end,
    type: type.sub(sub),
    block: block&.sub(sub),
    location: location
  )
end

#to_json(state = _ = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rbs/method_type.rb', line 24

def to_json(state = _ = nil)
  {
    type_params: type_params,
    type: type,
    block: block,
    location: location
  }.to_json(state)
end

#to_sObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rbs/method_type.rb', line 95

def to_s
  block_self_binding = Types::SelfTypeBindingHelper.self_type_binding_to_s(block&.self_type)

  s = case
      when (b = block) && b.required
        "(#{type.param_to_s}) { (#{b.type.param_to_s}) #{block_self_binding}-> #{b.type.return_to_s} } -> #{type.return_to_s}"
      when b = block
        "(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) #{block_self_binding}-> #{b.type.return_to_s} } -> #{type.return_to_s}"
      else
        "(#{type.param_to_s}) -> #{type.return_to_s}"
      end

  if type_params.empty?
    s
  else
    "[#{type_params.join(", ")}] #{s}"
  end
end

#type_param_namesObject



114
115
116
# File 'lib/rbs/method_type.rb', line 114

def type_param_names
  type_params.map(&:name)
end

#update(type_params: self.type_params, type: self.type, block: self.block, location: self.location) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/rbs/method_type.rb', line 48

def update(type_params: self.type_params, type: self.type, block: self.block, location: self.location)
  self.class.new(
    type_params: type_params,
    type: type,
    block: block,
    location: location
  )
end