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.



1403
1404
1405
1406
1407
1408
# File 'lib/rbs/types.rb', line 1403

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.



1399
1400
1401
# File 'lib/rbs/types.rb', line 1399

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



1401
1402
1403
# File 'lib/rbs/types.rb', line 1401

def location
  @location
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



1400
1401
1402
# File 'lib/rbs/types.rb', line 1400

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



1398
1399
1400
# File 'lib/rbs/types.rb', line 1398

def type
  @type
end

Instance Method Details

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



1410
1411
1412
# File 'lib/rbs/types.rb', line 1410

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

#each_type(&block) ⇒ Object



1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
# File 'lib/rbs/types.rb', line 1464

def each_type(&block)
  if block
    type.each_type(&block)
    yield self_type if self_type
    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



1420
1421
1422
1423
1424
1425
# File 'lib/rbs/types.rb', line 1420

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)


1503
1504
1505
# File 'lib/rbs/types.rb', line 1503

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

#has_self_type?Boolean

Returns:

  • (Boolean)


1499
1500
1501
# File 'lib/rbs/types.rb', line 1499

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

#hashObject



1416
1417
1418
# File 'lib/rbs/types.rb', line 1416

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

#map_type(&block) ⇒ Object



1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
# File 'lib/rbs/types.rb', line 1486

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



1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/rbs/types.rb', line 1477

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



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

def sub(s)
  return self if s.empty?

  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



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

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



1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
# File 'lib/rbs/types.rb', line 1448

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)


1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/rbs/types.rb', line 1507

def with_nonreturn_void?
  if type.with_nonreturn_void? || self_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