>> plot = function [coordinate = "cartesian" in {"cartesian", "polar"}, color_scheme = "bw" in {"bw", "rgb"}] (x, y) -> () ... ... end >> plot.color_scheme = "rgb"; // ok >> plot.color_scheme = "bs"; // !!! errorThe general syntax for specifying the domain of a parameter is
[access_control_type] parameter_name [= initial_value [in domain]]where initial_value is the initial value of the parameter, domain is the domain of the parameter. initial_value and domain are evaluated in the scope surrounding the function definition. If the domain is specified, then the value assigned to the parameter must belong to the domain. Otherwise an error will occur.
The value of domain may be a finite set, which is defined using a pair braces, like in the above sample function plot. Domains can also be functions that act like sets. For example, we may use the following function to define a set of positive numbers
pnumbers = x -> (x > 0);Then we may use this function as the domain of a parameter
lf = function [alpha = 1 in pnumbers, beta = 0 in pnumbers] x -> y y = alpha * x + beta; endNote that the set pnumbers can be represented using interval pnumbers = 0+ to inf, or using the built-in function _R.
By using domains we ensure that parameters can only be set to valid values, and then the function is never in an illegal state. Note that although you don't have to provide a domain in the function definition, every parameter always has a domain - the default domain for any parameter is the set that contains everything - _ALL. Since anything belongs to this set, having it as domain won't apply any restrictions to the values assigned to the parameter.