class Puppet::Pops::Types::PCallableType

@api public

Constants

DEFAULT

Attributes

block_type[R]

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. @return [PAnyType|nil] the block type

param_types[R]

Types of parameters as a Tuple with required/optional count, or an Integer with min (required), max count @return [PTupleType] the tuple representing the parameter types

return_type[R]

@return [PAnyType] The type for the values returned by this callable. Returns `nil` if return value is unconstrained

Public Class Methods

new(param_types, block_type = nil, return_type = nil) click to toggle source

@param param_types [PTupleType] @param block_type [PAnyType] @param return_type [PAnyType]

     # File lib/puppet/pops/types/types.rb
2378 def initialize(param_types, block_type = nil, return_type = nil)
2379   @param_types = param_types
2380   @block_type = block_type
2381   @return_type = return_type == PAnyType::DEFAULT ? nil : return_type
2382 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
2345 def self.register_ptype(loader, ir)
2346   create_ptype(loader, ir, 'AnyType',
2347     'param_types' => {
2348       KEY_TYPE => POptionalType.new(PTypeType.new(PTupleType::DEFAULT)),
2349       KEY_VALUE => nil
2350     },
2351     'block_type' => {
2352       KEY_TYPE => POptionalType.new(PTypeType.new(PCallableType::DEFAULT)),
2353       KEY_VALUE => nil
2354     },
2355     'return_type' => {
2356       KEY_TYPE => POptionalType.new(PTypeType::DEFAULT),
2357       KEY_VALUE => PAnyType::DEFAULT
2358     }
2359   )
2360 end

Public Instance Methods

accept(visitor, guard) click to toggle source
Calls superclass method Puppet::Pops::Types::PAnyType#accept
     # File lib/puppet/pops/types/types.rb
2384 def accept(visitor, guard)
2385   super
2386   @param_types.accept(visitor, guard) unless @param_types.nil?
2387   @block_type.accept(visitor, guard) unless @block_type.nil?
2388   @return_type.accept(visitor, guard) unless @return_type.nil?
2389 end
block_range() click to toggle source

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

     # File lib/puppet/pops/types/types.rb
2455 def block_range
2456   case block_type
2457   when POptionalType
2458     [0,1]
2459   when PVariantType, PCallableType
2460     [1,1]
2461   else
2462     [0,0]
2463   end
2464 end
callable_args?(required_callable_t, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2433 def callable_args?(required_callable_t, guard)
2434   # If the required callable is equal or more specific than self, self is acceptable arguments
2435   required_callable_t.assignable?(self, guard)
2436 end
callable_with?(args, block = nil) click to toggle source

Returns `true` if this instance is a callable that accepts the given args

@param args [Array] the arguments to test @return [Boolean] `true` if this instance is a callable that accepts the given args

     # File lib/puppet/pops/types/types.rb
2421 def callable_with?(args, block = nil)
2422   # nil param_types and compatible return type means other Callable is assignable
2423   return true if @param_types.nil?
2424   return false unless @param_types.instance?(args)
2425   if @block_type.nil?
2426     block == nil
2427   else
2428     @block_type.instance?(block)
2429   end
2430 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2470 def eql?(o)
2471   self.class == o.class && @param_types == o.param_types && @block_type == o.block_type && @return_type == o.return_type
2472 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2391 def generalize
2392   if self == DEFAULT
2393     DEFAULT
2394   else
2395     params_t = @param_types.nil? ? nil : @param_types.generalize
2396     block_t = @block_type.nil? ? nil : @block_type.generalize
2397     return_t = @return_type.nil? ? nil : @return_type.generalize
2398     @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : PCallableType.new(params_t, block_t, return_t)
2399   end
2400 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2466 def hash
2467   [@param_types, @block_type, @return_type].hash
2468 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2413 def instance?(o, guard = nil)
2414   (o.is_a?(Proc) || o.is_a?(Evaluator::Closure) || o.is_a?(Functions::Function)) && assignable?(TypeCalculator.infer(o), guard)
2415 end
kind_of_callable?(optional=true, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2438 def kind_of_callable?(optional=true, guard = nil)
2439   true
2440 end
last_range() click to toggle source

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

     # File lib/puppet/pops/types/types.rb
2449 def last_range
2450   @param_types.nil? ? nil : @param_types.repeat_last_range
2451 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2402 def normalize(guard = nil)
2403   if self == DEFAULT
2404     DEFAULT
2405   else
2406     params_t = @param_types.nil? ? nil : @param_types.normalize(guard)
2407     block_t = @block_type.nil? ? nil : @block_type.normalize(guard)
2408     return_t = @return_type.nil? ? nil : @return_type.normalize(guard)
2409     @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : PCallableType.new(params_t, block_t, return_t)
2410   end
2411 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2474 def resolve(loader)
2475   params_t = @param_types.nil? ? nil : @param_types.resolve(loader)
2476   block_t = @block_type.nil? ? nil : @block_type.resolve(loader)
2477   return_t = @return_type.nil? ? nil : @return_type.resolve(loader)
2478   @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : self.class.new(params_t, block_t, return_t)
2479 end
size_range() click to toggle source

Returns the number of accepted arguments [min, max]

     # File lib/puppet/pops/types/types.rb
2443 def size_range
2444   @param_types.nil? ? nil : @param_types.size_range
2445 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2486 def _assignable?(o, guard)
2487   return false unless o.is_a?(PCallableType)
2488   return false unless @return_type.nil? || @return_type.assignable?(o.return_type || PAnyType::DEFAULT, guard)
2489 
2490   # nil param_types and compatible return type means other Callable is assignable
2491   return true if @param_types.nil?
2492 
2493   # NOTE: these tests are made in reverse as it is calling the callable that is constrained
2494   # (it's lower bound), not its upper bound
2495   other_param_types = o.param_types
2496 
2497   return false if other_param_types.nil? ||  !other_param_types.assignable?(@param_types, guard)
2498   # names are ignored, they are just information
2499   # Blocks must be compatible
2500   this_block_t = @block_type || PUndefType::DEFAULT
2501   that_block_t = o.block_type || PUndefType::DEFAULT
2502   that_block_t.assignable?(this_block_t, guard)
2503 end