Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-5845

Add the function type()

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: PUP 4.4.0
    • Component/s: None
    • Labels:
      None
    • Template:
    • Epic Link:
    • Story Points:
      1
    • Sprint:
      Language 2016-02-24
    • Release Notes:
      New Feature
    • Release Notes Summary:
      Hide
      The function type(x, inference_type = detailed) has been added. It produces the type of the given argument. This function removes the need to use the standard lib function type_of which does the same thing. The function can produce generalized, reduced or detailed type information. This is useful when producing custom error messages when a type check fails.
      Show
      The function type(x, inference_type = detailed) has been added. It produces the type of the given argument. This function removes the need to use the standard lib function type_of which does the same thing. The function can produce generalized, reduced or detailed type information. This is useful when producing custom error messages when a type check fails.

      Description

      Add the function type(x, inference_type = detailed) that produces the type of the given argument.

      The puppet data type is produced by the function using an inference algorithm of given inference_type. The default is to use full detailed inference.

      The quality optional parameter can be one of:

      • 'generalized' - reduces type and removes length constraints
      • 'reduced' - reduces types to common types in collections
      • 'detailed' - retains all inferred types with highest possible quality

      See documentation of the function for details and examples.

      There is currently a function named type_of in stdlib. This is the same basic functionality but available in core as it is odd to not have this directly in core. The new function also gives the ability to do inference of a degree of generality

      This was made possible in the work done on PUP-5742 as the type keyword was changed from just being reserved for future use.

      USAGE


      Users are encouraged to use assert_type with default error message, but when writing custom error messages the type function is required in order to describe what was wrong if the value did not match the expected type. i.e. the typical use is:

      define example() {
        $x = some_function()
        unless arg =~ Integer {
          fail("Expected some_function() to produce an Integer, but produced a value of type:' ${ type($x) }'")
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              henrik.lindberg Henrik Lindberg
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Zendesk Support