Sha256: c2756474627a5d718725d66bb9fff88493dfd3eb2f7d75a050e05f409b3717e2

Contents?: true

Size: 1.92 KB

Versions: 295

Compression:

Stored size: 1.92 KB

Contents

# Returns the data type of a given value with a given degree of generality.
#
# ```puppet
# type InferenceFidelity = Enum[generalized, reduced, detailed]
#
# function type(Any $value, InferenceFidelity $fidelity = 'detailed') # returns Type
# ```
#
# @example Using `type`
#
# ``` puppet
# notice type(42) =~ Type[Integer]
# ```
#
# Would notice `true`.
#
# By default, the best possible inference is made where all details are retained.
# This is good when the type is used for further type calculations but is overwhelmingly
# rich in information if it is used in a error message.
#
# The optional argument `$fidelity` may be given as (from lowest to highest fidelity):
#
# * `generalized` - reduces to common type and drops size constraints
# * `reduced` - reduces to common type in collections
# * `detailed` - (default) all details about inferred types is retained
#
# @example Using `type()` with different inference fidelity:
#
# ``` puppet
# notice type([3.14, 42], 'generalized')
# notice type([3.14, 42], 'reduced'')
# notice type([3.14, 42], 'detailed')
# notice type([3.14, 42])
# ```
#
# Would notice the four values:
#
# 1. 'Array[Numeric]'
# 2. 'Array[Numeric, 2, 2]'
# 3. 'Tuple[Float[3.14], Integer[42,42]]]'
# 4. 'Tuple[Float[3.14], Integer[42,42]]]'
#
# @since 4.4.0
#
Puppet::Functions.create_function(:type) do
  dispatch :type_detailed do
    param 'Any', :value
    optional_param 'Enum[detailed]', :inference_method
  end

  dispatch :type_parameterized do
    param 'Any', :value
    param 'Enum[reduced]', :inference_method
  end

  dispatch :type_generalized do
    param 'Any', :value
    param 'Enum[generalized]', :inference_method
  end

  def type_detailed(value, _ = nil)
    Puppet::Pops::Types::TypeCalculator.infer_set(value)
  end

  def type_parameterized(value, _)
    Puppet::Pops::Types::TypeCalculator.infer(value)
  end

  def type_generalized(value, _)
    Puppet::Pops::Types::TypeCalculator.infer(value).generalize
  end
end

Version data entries

295 entries across 295 versions & 2 rubygems

Version Path
puppet-6.22.1 lib/puppet/functions/type.rb
puppet-6.22.1-x86-mingw32 lib/puppet/functions/type.rb
puppet-6.22.1-x64-mingw32 lib/puppet/functions/type.rb
puppet-6.22.1-universal-darwin lib/puppet/functions/type.rb
puppet-7.6.1 lib/puppet/functions/type.rb
puppet-7.6.1-x86-mingw32 lib/puppet/functions/type.rb
puppet-7.6.1-x64-mingw32 lib/puppet/functions/type.rb
puppet-7.6.1-universal-darwin lib/puppet/functions/type.rb
puppet-7.5.0 lib/puppet/functions/type.rb
puppet-7.5.0-x86-mingw32 lib/puppet/functions/type.rb
puppet-7.5.0-x64-mingw32 lib/puppet/functions/type.rb
puppet-7.5.0-universal-darwin lib/puppet/functions/type.rb
puppet-7.4.1 lib/puppet/functions/type.rb
puppet-7.4.1-x86-mingw32 lib/puppet/functions/type.rb
puppet-7.4.1-x64-mingw32 lib/puppet/functions/type.rb
puppet-7.4.1-universal-darwin lib/puppet/functions/type.rb
puppet-6.21.1 lib/puppet/functions/type.rb
puppet-6.21.1-x86-mingw32 lib/puppet/functions/type.rb
puppet-6.21.1-x64-mingw32 lib/puppet/functions/type.rb
puppet-6.21.1-universal-darwin lib/puppet/functions/type.rb