|
There is currently a type_of function in standard lib that returns the type of its argument, but we have no expression in the language that evaluates to a reference to a parameter (we can only pass evaluated values).
The main problem with supporting getting the type of the parameter is how to express that reference. The current naming rules requires each segment to begin with the same capitalization; lowercase means instance reference ('the value of' as in $apache::vhost::ensure), and uppercase means type references. We currently do not allow $Apache::Vhost which (if we allowed it would return the instance of the Resource Type (as opposed to Apache::Vhost which returns its type). From there we have two choices; either let instances of Resource type escape into the language so we access details e.g. $Apache::Vhost[ensure], or only accept that such references end with a lower case reference to the parameter, and then we can use $ to access it i.e. $Apache::Vhost::ensure. IMO the distinction between upper/lowercase here is too subtle and it is too easy to make a type and get something completely different as a result. One solution is to use a different prefix - say & to mean "reference to" (this is useful to reference other things as well, such as functions (with the intent of passing the function as a block to another function)). In short - $ is a reference to a value, & is a reference to a thing that has or produces a value. We can then use all lower case e.g. &apache::vhost::param.
The next hurdle is what such a reference really means. It seems natural that it evaluates to the object that describes the param (and 'param_type' is an attribute of that param). This means if we call type_of(&apache::vhost::param) we would get something like Param[Enum[...]], and we would need a way to reference the type parameters (i.e. the type of the param). We currently have no way to get the type parameters of a type. So this requires additional language features and or changes (remember that things like Package['foo'] is a reference to a package resource, the 'foo' is not navigation to an attribute of Package - the way this was originally designed in Puppet squats on the semantics of the [] operator for resources and there are too many special cases to make that work. The '.' operator is also used to mean "call function" - we could make the function name resolution also consider attribute names as the names of getter functions and that they have higher precedence than regular functions (while it would work, it is not very appealing).
While it should be possible to come up with an elegant solution, it may not be very easy to understand. I am therefore leaning towards that it is better to introduce a specific function to get the type of a parameter:
# Using string name of the parameter
|
$the_type = param_type(apache::vhost::ensure)
|
|
# Using a type reference, and the name of a parameter
|
$the_type = param_type(Apache::Vhost, ensure)
|
This however creates a different problem - we cannot type a parameter by using a function; it must be a type reference. We could however use a type alias like this:
type = MyModule::ApacheVhost::Ensure = param_type(apache::vhost::ensure)
|
define( MyModule::ApacheVhost::Ensure $ensure) {
|
...
|
}
|
|