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.



1355
1356
1357
1358
1359
1360
# File 'lib/rbs/types.rb', line 1355

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.



1351
1352
1353
# File 'lib/rbs/types.rb', line 1351

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



1353
1354
1355
# File 'lib/rbs/types.rb', line 1353

def location
  @location
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



1352
1353
1354
# File 'lib/rbs/types.rb', line 1352

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



1350
1351
1352
# File 'lib/rbs/types.rb', line 1350

def type
  @type
end

Instance Method Details

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



1362
1363
1364
# File 'lib/rbs/types.rb', line 1362

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

#each_type(&block) ⇒ Object



1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
# File 'lib/rbs/types.rb', line 1414

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



1372
1373
1374
1375
1376
1377
# File 'lib/rbs/types.rb', line 1372

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)


1452
1453
1454
# File 'lib/rbs/types.rb', line 1452

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

#has_self_type?Boolean

Returns:

  • (Boolean)


1448
1449
1450
# File 'lib/rbs/types.rb', line 1448

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

#hashObject



1368
1369
1370
# File 'lib/rbs/types.rb', line 1368

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

#map_type(&block) ⇒ Object



1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
# File 'lib/rbs/types.rb', line 1435

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



1426
1427
1428
1429
1430
1431
1432
1433
# File 'lib/rbs/types.rb', line 1426

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



1389
1390
1391
1392
1393
1394
1395
1396
# File 'lib/rbs/types.rb', line 1389

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



1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'lib/rbs/types.rb', line 1379

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



1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
# File 'lib/rbs/types.rb', line 1398

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)


1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
# File 'lib/rbs/types.rb', line 1456

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