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.



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

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.



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

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



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

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



1396
1397
1398
# File 'lib/rbs/types.rb', line 1396

def type
  @type
end

Instance Method Details

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



1408
1409
1410
# File 'lib/rbs/types.rb', line 1408

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

#each_type(&block) ⇒ Object



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

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



1418
1419
1420
1421
1422
1423
# File 'lib/rbs/types.rb', line 1418

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)


1501
1502
1503
# File 'lib/rbs/types.rb', line 1501

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

#has_self_type?Boolean

Returns:

  • (Boolean)


1497
1498
1499
# File 'lib/rbs/types.rb', line 1497

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

#hashObject



1414
1415
1416
# File 'lib/rbs/types.rb', line 1414

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

#map_type(&block) ⇒ Object



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

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



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

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



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

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



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

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



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

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)


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

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