Details
-
Improvement
-
Status: Closed
-
Normal
-
Resolution: Fixed
-
None
-
None
-
None
-
1
-
Language 2016-02-24
-
New Feature
-
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) }'") |
}
|
}
|