Class: RBS::Types::Proc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location:, type:, block:, self_type: nil) ⇒ Proc

Returns a new instance of Proc.



1241
1242
1243
1244
1245
1246
# File 'lib/rbs/types.rb', line 1241

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



1237
1238
1239
# File 'lib/rbs/types.rb', line 1237

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



1239
1240
1241
# File 'lib/rbs/types.rb', line 1239

def location
  @location
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



1238
1239
1240
# File 'lib/rbs/types.rb', line 1238

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



1236
1237
1238
# File 'lib/rbs/types.rb', line 1236

def type
  @type
end

Instance Method Details

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



1248
1249
1250
# File 'lib/rbs/types.rb', line 1248

def ==(other)
  other.is_a?(Proc) && other.type == type && other.block == block && other.self_type == self_type
end

#each_type(&block) ⇒ Object



1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'lib/rbs/types.rb', line 1300

def each_type(&block)
  if block
    type.each_type(&block)
    self.block&.type&.each_type(&block)
    if self_type = self.block&.self_type
      yield self_type
    end
  else
    enum_for :each_type
  end
end

#free_variables(set = ) ⇒ Object



1258
1259
1260
1261
1262
1263
# File 'lib/rbs/types.rb', line 1258

def free_variables(set = Set[])
  type.free_variables(set)
  block&.type&.free_variables(set)
  self_type&.free_variables(set)
  set
end

#has_classish_type?Boolean

Returns:

  • (Boolean)


1338
1339
1340
# File 'lib/rbs/types.rb', line 1338

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

#has_self_type?Boolean

Returns:

  • (Boolean)


1334
1335
1336
# File 'lib/rbs/types.rb', line 1334

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

#hashObject



1254
1255
1256
# File 'lib/rbs/types.rb', line 1254

def hash
  self.class.hash ^ type.hash ^ block.hash ^ self_type.hash
end

#map_type(&block) ⇒ Object



1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
# File 'lib/rbs/types.rb', line 1321

def map_type(&block)
  if block
    Proc.new(
      type: type.map_type(&block),
      block: self.block&.map_type(&block),
      self_type: self_type ? yield(self_type) : nil,
      location: location
    )
  else
    enum_for :map_type
  end
end

#map_type_name(&block) ⇒ Object



1312
1313
1314
1315
1316
1317
1318
1319
# File 'lib/rbs/types.rb', line 1312

def map_type_name(&block)
  Proc.new(
    type: type.map_type_name(&block),
    block: self.block&.map_type {|type| type.map_type_name(&block) },
    self_type: self_type&.map_type_name(&block),
    location: location
  )
end

#sub(s) ⇒ Object



1275
1276
1277
1278
1279
1280
1281
1282
# File 'lib/rbs/types.rb', line 1275

def sub(s)
  self.class.new(
    type: type.sub(s),
    block: block&.sub(s),
    self_type: self_type&.sub(s),
    location: location
  )
end

#to_json(state = _ = nil) ⇒ Object



1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/rbs/types.rb', line 1265

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

#to_s(level = 0) ⇒ Object



1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
# File 'lib/rbs/types.rb', line 1284

def to_s(level = 0)
  self_binding = SelfTypeBindingHelper.self_type_binding_to_s(self_type)
  block_self_binding = SelfTypeBindingHelper.self_type_binding_to_s(block&.self_type)

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

#with_nonreturn_void?Boolean

Returns:

  • (Boolean)


1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
# File 'lib/rbs/types.rb', line 1342

def with_nonreturn_void?
  if type.with_nonreturn_void?
    true
  else
    if block = block()
      block.type.with_nonreturn_void? || block.self_type&.with_nonreturn_void? || false
    else
      false
    end
  end
end