Class: Puppet::Pal::FunctionSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pal/function_signature.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(function_class) ⇒ FunctionSignature

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 a new instance of FunctionSignature


13
14
15
# File 'lib/puppet/pal/function_signature.rb', line 13

def initialize(function_class)
  @func = function_class
end

Instance Method Details

#callable_with?(args, callable = nil) {|String| ... } ⇒ Boolean

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.

Yields:

  • (String)

    a formatted error message describing a type mismatch if the function is not callable with given args + block


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet/pal/function_signature.rb', line 28

def callable_with?(args, callable=nil)
  signatures = @func.dispatcher.to_type
  callables = signatures.is_a?(Puppet::Pops::Types::PVariantType) ? signatures.types : [signatures]

  return true if callables.any? {|t| t.callable_with?(args) }
  return false unless block_given?
  args_type = Puppet::Pops::Types::TypeCalculator.singleton.infer_set(callable.nil? ? args : args + [callable])
  error_message = Puppet::Pops::Types::TypeMismatchDescriber.describe_signatures(@func.name, @func.signatures, args_type)
  yield error_message
  false
end

#callablesArray<Puppet::Pops::Types::PCallableType] one callable per way the function can be called

Returns an array of Callable puppet data type


45
46
47
48
# File 'lib/puppet/pal/function_signature.rb', line 45

def callables
  signatures = @func.dispatcher.to_type
  signatures.is_a?(Puppet::Pops::Types::PVariantType) ? signatures.types : [signatures]
end