On this page:
Parameter
Parameter.make
Parameter.def
parameterize
8.12

6.17 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 dyanmic binding or operating-system environment variable—but a context parameter is a value, not a variable. Create a context parameter using Parameter.make, get a context parameter’s value by calling the parameter as a function of zero arguments, and set a context parameter’s value using parameterize.

annotation

Parameter

A context parameter also satisfies Function and Function.of_arity(0, 1).

function

fun Parameter.make(

  initial_val :: Any,

  ~name: name :: Symbol = #'parameter,

  ~guard: guard :: maybe(Function.of_arity(1)) = #false

) :: Parameter

 

definition

Parameter.def id_name maybe_annot = expr

 

definition

Parameter.def id_name maybe_annot:

  body

  ...

 

maybe_annot

 = 

:: annot

 | 

:~ annot

 | 

ϵ

The Parameter.make function creates a context parameter whose initial value is initial_val and whose name for debugging purposes as name. If guard is not #false, then it used as a filter on any candidate value for the function, whether that value is supplied via parameterize or by calling the context parameter as a function with one argument. The guard filter is not applied to initial_val.

A context parameter acts 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 procedure 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.

def size = Parameter.make(10)

> size()

10

> size(11)

> size()

11

Parameter.def color :: String = "red"

> use_static

> 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

parameterize { parameter_expr:

                 val_body

                 ...,

               ... }:

  body

  ...

Returns the result of the body block as evaluated in a fresh parameterization that binds the context parameter produced by each parameter_expr to the value produced by the corresponding val_body sequence.

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