Class: Puppet::Pops::Types::PCallableType

Inherits:
PAnyType show all
Defined in:
lib/puppet/pops/types/types.rb

Constant Summary collapse

DEFAULT =
PCallableType.new(nil)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #iterable?, #iterable_type, #simple_name, #to_alias_expanded_s, #to_s

Constructor Details

#initialize(param_types, block_type = nil) ⇒ PCallableType

Returns a new instance of PCallableType.

Parameters:



1503
1504
1505
1506
# File 'lib/puppet/pops/types/types.rb', line 1503

def initialize(param_types, block_type = nil)
  @param_types = param_types
  @block_type = block_type
end

Instance Attribute Details

#block_typePAnyType|nil (readonly)

Although being an abstract type reference, only Callable, or all Callables wrapped in Optional or Variant are supported If not set, the meaning is that block is not supported.

Returns:



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

def block_type
  @block_type
end

#param_typesPTupleType (readonly)

Types of parameters as a Tuple with required/optional count, or an Integer with min (required), max count

Returns:

  • (PTupleType)

    the tuple representing the parameter types



1493
1494
1495
# File 'lib/puppet/pops/types/types.rb', line 1493

def param_types
  @param_types
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



1508
1509
1510
1511
1512
# File 'lib/puppet/pops/types/types.rb', line 1508

def accept(visitor, guard)
  super
  @param_types.accept(visitor, guard) unless @param_types.nil?
  @block_type.accept(visitor, guard) unless @block_type.nil?
end

#block_rangeObject

Range [0,0], [0,1], or [1,1] for the block



1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
# File 'lib/puppet/pops/types/types.rb', line 1561

def block_range
  case block_type
  when POptionalType
    [0,1]
  when PVariantType, PCallableType
    [1,1]
  else
    [0,0]
  end
end

#callable_args?(required_callable_t, guard) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


1539
1540
1541
1542
# File 'lib/puppet/pops/types/types.rb', line 1539

def callable_args?(required_callable_t, guard)
  # If the required callable is euqal or more specific than self, self is acceptable arguments
  required_callable_t.assignable?(self, guard)
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1576
1577
1578
# File 'lib/puppet/pops/types/types.rb', line 1576

def eql?(o)
  self.class == o.class && @param_types == o.param_types && @block_type == o.block_type
end

#generalizeObject



1514
1515
1516
1517
1518
1519
1520
1521
1522
# File 'lib/puppet/pops/types/types.rb', line 1514

def generalize
  if self == DEFAULT
    DEFAULT
  else
    params_t = @param_types.nil? ? nil : @param_types.generalize
    block_t = @block_type.nil? ? nil : @block_type.generalize
    @param_types.equal?(params_t) && @block_type.equal?(block_t) ? self : PCallableType.new(params_t, block_t)
  end
end

#hashObject



1572
1573
1574
# File 'lib/puppet/pops/types/types.rb', line 1572

def hash
  @param_types.hash ^ @block_type.hash
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


1534
1535
1536
# File 'lib/puppet/pops/types/types.rb', line 1534

def instance?(o)
  assignable?(TypeCalculator.infer(o))
end

#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1544
1545
1546
# File 'lib/puppet/pops/types/types.rb', line 1544

def kind_of_callable?(optional=true, guard = nil)
    true
end

#last_rangeObject

Returns the number of accepted arguments for the last parameter type [min, max]



1555
1556
1557
# File 'lib/puppet/pops/types/types.rb', line 1555

def last_range
  @param_types.nil? ? nil : @param_types.repeat_last_range
end

#normalize(guard = nil) ⇒ Object



1524
1525
1526
1527
1528
1529
1530
1531
1532
# File 'lib/puppet/pops/types/types.rb', line 1524

def normalize(guard = nil)
  if self == DEFAULT
    DEFAULT
  else
    params_t = @param_types.nil? ? nil : @param_types.normalize(guard)
    block_t = @block_type.nil? ? nil : @block_type.normalize(guard)
    @param_types.equal?(params_t) && @block_type.equal?(block_t) ? self : PCallableType.new(params_t, block_t)
  end
end

#size_rangeObject

Returns the number of accepted arguments [min, max]



1549
1550
1551
# File 'lib/puppet/pops/types/types.rb', line 1549

def size_range
  @param_types.nil? ? nil : @param_types.size_range
end