A parameter object is a procedure that is bound to a location, and may optionally have a conversion procedure. The procedure accepts zero or one argument. When the procedure is called with zero arguments, the content of the location is returned. On a call with one argument the content of the location is updated with the result of applying the parameter object’s conversion procedure to the argument.
Parameter objects are created with the make-parameter
procedure
which takes one or two arguments. The second argument is a one
argument conversion procedure. If only one argument is passed to
make-parameter the identity function is used as a conversion
procedure.
A new location is created and asociated with the
parameter object. The initial content of the location is the
result of applying the conversion procedure to the first argument of
make-parameter.
Note that the conversion procedure can be used for guaranteeing the type of the parameter object’s binding and/or to perform some conversion of the value.
The parameterize
special form, when given a parameter object
and a value, binds the parameter
object to a new location for the dynamic extent of its body.
The initial content of the location is the result of
applying the parameter object’s conversion procedure to the value. The
parameterize
special form behaves analogously to let
when binding more than one parameter object (that is the order of
evaluation is unspecified and the new bindings are only visible in the
body of the parameterize special form).
When a new thread is created using future
or runnable
then the child thread inherits initial values from its parent.
Once the child is running, changing the value in the child does not
affect the value in the parent or vice versa.
(In the past this was not the case: The child would share a location
with the parent except within a parameterize
.
This was changed to avoid unsafe and inefficient coupling between threads.)
Note that parameterize
and fluid-let
have similar
binding and sharing behavior.
The difference is that fluid-let
modifies locations
accessed by name, while make-parameter
and parameterize
create anonymous locations accessed by calling a parameter procedure.
The R5RS procedures current-input-port
and current-output-port
are parameter objects.
Procedure: make-parameter
init
[converter
]
Returns a new parameter object which is bound in the global dynamic environment to a location containing the value returned by the call
(
. If the conversion procedure converter is not specified the identity function is used instead.converter
init
)The parameter object is a procedure which accepts zero or one argument. When it is called with no argument, the content of the location bound to this parameter object in the current dynamic environment is returned. When it is called with one argument, the content of the location is set to the result of the call
(
, whereconverter
arg
)arg
is the argument passed to the parameter object, and an unspecified value is returned.(define radix (make-parameter 10)) (define write-shared (make-parameter #f (lambda (x) (if (boolean? x) x (error "only booleans are accepted by write-shared"))))) (radix) ⇒ 10 (radix 2) (radix) ⇒ 2 (write-shared 0) gives an error (define prompt (make-parameter 123 (lambda (x) (if (string? x) x (with-output-to-string (lambda () (write x))))))) (prompt) ⇒ "123" (prompt ">") (prompt) ⇒ ">"
Syntax: parameterize
((expr1
expr2
) ...
) body
The expressions
expr1
andexpr2
are evaluated in an unspecified order. The value of theexpr1
expressions must be parameter objects. For eachexpr1
expression and in an unspecified order, the local dynamic environment is extended with a binding of the parameter objectexpr1
to a new location whose content is the result of the call(
, whereconverter
val
)val
is the value ofexpr2
andconverter
is the conversion procedure of the parameter object. The resulting dynamic environment is then used for the evaluation ofbody
(which refers to the R5RS grammar nonterminal of that name). The result(s) of the parameterize form are the result(s) of thebody
.(radix) ⇒ 2 (parameterize ((radix 16)) (radix)) ⇒ 16 (radix) ⇒ 2 (define (f n) (number->string n (radix))) (f 10) ⇒ "1010" (parameterize ((radix 8)) (f 10)) ⇒ "12" (parameterize ((radix 8) (prompt (f 10))) (prompt)) ⇒ "1010"