Class: RBS::MethodType

Inherits:
Object
  • Object
show all
Includes:
_ToJson
Defined in:
lib/rbs/method_type.rb,
sig/method_types.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MethodType.

Parameters:



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

#blockTypes::Block? (readonly)

Returns the value of attribute block.

Returns:



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

def block
  @block
end

#locationloc? (readonly)

Returns the value of attribute location.

Returns:

  • (loc, nil)


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

def location
  @location
end

#typeTypes::function (readonly)

Returns the value of attribute type.

Returns:

  • (Types::function)


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

def type
  @type
end

#type_paramsArray[AST::TypeParam] (readonly)

Returns the value of attribute type_params.

Returns:



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

def type_params
  @type_params
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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_typevoid #each_typeEnumerator[Types::t, void]

Overloads:

  • #each_typevoid

    This method returns an undefined value.

  • #each_typeEnumerator[Types::t, void]

    Returns:

    • (Enumerator[Types::t, void])

Yields:

Yield Parameters:

  • arg0 (Types::t)

Yield Returns:

  • (void)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rbs/method_type.rb', line 86

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

#free_variables(set = Set.new) ⇒ Set[Symbol]

Parameters:

  • set (Set[Symbol]) (defaults to: Set.new)

Returns:

  • (Set[Symbol])


59
60
61
62
63
# File 'lib/rbs/method_type.rb', line 59

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

#has_classish_type?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rbs/method_type.rb', line 127

def has_classish_type?
  each_type.any? {|type| type.has_classish_type? }
end

#has_self_type?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/rbs/method_type.rb', line 123

def has_self_type?
  each_type.any? {|type| type.has_self_type? }
end

#map_type {|arg0| ... } ⇒ MethodType

Apply the mapping included in the MethodType.

Note that type bound in generics parameter is not handled by this method. You may want to use #map_type_bound explicitly, or #sub for simple substitution.

Yields:

Yield Parameters:

  • arg0 (Types::t)

Yield Returns:

  • (Types::t)

Returns:



41
42
43
44
45
46
47
48
# File 'sig/method_types.rbs', line 41

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 {|arg0| ... } ⇒ MethodType

Yields:

Yield Parameters:

  • arg0 (Types::t)

Yield Returns:

  • (Types::t)

Returns:



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

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) ⇒ MethodType

Substitute type variables to some types. Takes care of type parameter bounds.

Parameters:

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'sig/method_types.rbs', line 30

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

  return self if sub.empty?

  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_sString

Returns:

  • (String)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rbs/method_type.rb', line 100

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_namesArray[Symbol]

Returns:

  • (Array[Symbol])


119
120
121
# File 'lib/rbs/method_type.rb', line 119

def type_param_names
  type_params.map(&:name)
end

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

Parameters:

  • type_params: (Array[AST::TypeParam]) (defaults to: self.type_params)
  • type: (Types::function) (defaults to: self.type)
  • block: (Types::Block, nil) (defaults to: self.block)
  • location: (loc, nil) (defaults to: self.location)

Returns:



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

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

#with_nonreturn_void?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rbs/method_type.rb', line 131

def with_nonreturn_void?
  if type.with_nonreturn_void? # steep:ignore DeprecatedReference
    true
  else
    if block = block()
      block.type.with_nonreturn_void? || # steep:ignore DeprecatedReference
        block.self_type&.with_nonreturn_void? || # steep:ignore DeprecatedReference
        false
    else
      false
    end
  end
end