class Puppet::Pal::FunctionSignature

A FunctionSignature is returned from `function_signature`. Its purpose is to answer questions about the function's parameters and if it can be called with a set of parameters.

It is also possible to get an array of puppet Callable data type where each callable describes one possible way the function can be called.

@api public

Public Class Methods

new(function_class) click to toggle source

@api private

   # File lib/puppet/pal/function_signature.rb
13 def initialize(function_class)
14   @func = function_class
15 end

Public Instance Methods

callable_with?(args, callable=nil) { |error_message| ... } click to toggle source

Returns true if the function can be called with the given arguments and false otherwise. If the function is not callable, and a code block is given, it is given a formatted error message that describes the type mismatch. That error message can be quite complex if the function has multiple dispatch depending on given types.

@param args [Array] The arguments as given to the function call @param callable [Proc, nil] An optional ruby Proc or puppet lambda given to the function @yield [String] a formatted error message describing a type mismatch if the function is not callable with given args + block @return [Boolean] true if the function can be called with given args + block, and false otherwise @api public

   # File lib/puppet/pal/function_signature.rb
28 def callable_with?(args, callable=nil)
29   signatures = @func.dispatcher.to_type
30   callables = signatures.is_a?(Puppet::Pops::Types::PVariantType) ? signatures.types : [signatures]
31 
32   return true if callables.any? {|t| t.callable_with?(args) }
33   return false unless block_given?
34   args_type = Puppet::Pops::Types::TypeCalculator.singleton.infer_set(callable.nil? ? args : args + [callable])
35   error_message = Puppet::Pops::Types::TypeMismatchDescriber.describe_signatures(@func.name, @func.signatures, args_type)
36   yield error_message
37   false
38 end
callables() click to toggle source

Returns an array of Callable puppet data type @return [Array<Puppet::Pops::Types::PCallableType] one callable per way the function can be called

@api public

   # File lib/puppet/pal/function_signature.rb
45 def callables
46   signatures = @func.dispatcher.to_type
47   signatures.is_a?(Puppet::Pops::Types::PVariantType) ? signatures.types : [signatures]
48 end