Class: RBS::MethodType

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

Defined Under Namespace

Classes: Block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MethodType.



38
39
40
41
42
43
# File 'lib/rbs/method_type.rb', line 38

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.



35
36
37
# File 'lib/rbs/method_type.rb', line 35

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



36
37
38
# File 'lib/rbs/method_type.rb', line 36

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



34
35
36
# File 'lib/rbs/method_type.rb', line 34

def type
  @type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



33
34
35
# File 'lib/rbs/method_type.rb', line 33

def type_params
  @type_params
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
50
# File 'lib/rbs/method_type.rb', line 45

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

#each_type(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/rbs/method_type.rb', line 95

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



78
79
80
81
82
# File 'lib/rbs/method_type.rb', line 78

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

#map_type(&block) ⇒ Object



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

def map_type(&block)
  self.class.new(
    type_params: type_params,
    type: type.map_type(&block),
    block: self.block&.yield_self do |b|
      Block.new(type: b.type.map_type(&block), required: b.required)
    end,
    location: location
  )
end

#sub(s) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rbs/method_type.rb', line 61

def sub(s)
  s.without(*type_params).yield_self do |sub|
    map_type do |ty|
      ty.sub(sub)
    end
  end
end

#to_json(*a) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/rbs/method_type.rb', line 52

def to_json(*a)
  {
    type_params: type_params,
    type: type,
    block: block,
    location: location
  }.to_json(*a)
end

#to_sObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rbs/method_type.rb', line 106

def to_s
  s = case
      when (b = block) && b.required
        "(#{type.param_to_s}) { (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
      when b = block
        "(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) -> #{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

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



69
70
71
72
73
74
75
76
# File 'lib/rbs/method_type.rb', line 69

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