10.4 Context Parameters
A context parameter is a way of communicating a value from one
evaluation context to another one that is nested dynamically, but not
necessarily lexically. In other words, a context parameter is similar to
a dynamic binding or operating-system environment variable—
annotation | |
A context parameter also satisfies Function and Function.of_arity(0, 1).
function | ||||||||||||
| ||||||||||||
| ||||||||||||
definition | ||||||||||||
| ||||||||||||
definition | ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
|
A context parameter acts as a function that returns the parameter’s value in the current dynamic context when it is called with zero arguments, and it mutates the parameter’s value when called with one argument.
The Parameter.def form defines id_name to a new context parameter whose initial value is produced by expr or a body sequence. If an annotation is provided using ::, then the annotation’s predicate or conversion is used for a filter argument like the guard function for Parameter.make. If an annotation is provided with either :: or :~, static information from the annotation is associated with the result of calling id_name.
The ~name option in Parameter.def is like the ~name option for fun.
def size = Parameter.make(10)
> size()
10
> size(11)
> size()
11
Parameter.def color :: String = "red"
> color().length()
3
> color(5)
color: value does not satisfy annotation
value: 5
annotation: String
> parameterize { color: 5 }:
"ok"
color: value does not satisfy annotation
value: 5
annotation: String
expression | ||||||
|
If a context parameter for a parameter_expr is mutated during the evaluation of body, the mutation is not preserved outside of the evaluation of the body. That is, context-parameter mutation affects a particular parameterization, not the context parameter more globally.
def size = Parameter.make(10)
> size()
10
> parameterize { size: 11 }:
size()
11
> parameterize { size: 12 }:
size(13)
size()
13
> size()
10