For Gauche 0.9.10

Next: , Previous: , Up: Core library   [Contents][Index]

6.16 Parameters

A parameter is Scheme’s way to implement dynamically scoped states. In Gauche, it also realizes thread-local storage. It is codified in srfi-39 and incorporated into R7RS.

It is a special procedure created by make-parameter. It accepts zero or one argument. When called without an argument, it returns the current value. If an argument is passed, it alters its current value with the given value, and returns the previous value.

Using parameterize macro, which is similar to let syntactially, rebinds the parameters’ value during execution of its body, dynamically rather than lexically.

(define var (make-parameter 0))

(define (show-var) (print (var)))

(show-var)  ; prints 0

(parameterize ((var 1))
  (show-var))    ; prints 1

(define f)

(parameterize ((var 2))
  (set! f (lambda () (show-var))))

(f) ; prints 0, since its out of the dynamic extent of var=2

If a control trasfers from or to parameterize’s body by continuations, the parameter refers to the value corresponding the dynamic envrionment it is accessed.

Gauche enhances R7RS parameters in a few ways.

Function: make-parameter value :optional filter

[R7RS base] Creates a parameter whose initial value is value. If an optional argument filter is given, it must be a procedure that takes one argument and returns one value; whenever the parameter’s value is about to change, the procedure is called with the given value, and the value the procedure returns will be the parameter’s value. The filter procedure can raise an error or reject to change the parameter’s value.

NB: In 0.9.9 and before, this procedure returns a parameter object, and we used object-apply method to make it behave like a procedure. However, R7RS explicitly defines the return values to be a procedure; notably, portable code expects (procedure? (make-parameter 'z)) returns #t.

As of 0.9.10, we switched make-procedure to return a procedure. It won’t change the external behavior, except when you test the parameter p with (is-a? p <parameter>); you have to use parameter? instead.

Macro: parameterize ((param value) …) body …

[R7RS base] Evaluates body …, with change parameter param’s value to the given value within the dynamic scope of body …. Returns the value(s) of the result of the last body.

Some examples:

(define a (make-parameter 1))
(a) ⇒ 1
(a 2) ⇒ 1
(a) ⇒ 2
(parameterize ((a 3))
  (a)) ⇒ 3
(a) ⇒ 2
Function: parameter? obj

Returns #t iff obj is an object created by make-parameter.

Note: Some built-in procedures such as current-input-port behaves like parameters. They aren’t created by make-parameter, though, and returns #f for parameter?.

Next: , Previous: , Up: Core library   [Contents][Index]