[plt-scheme] Parameterizing Modules with variables
At Sun, 20 Apr 2003 05:51:21 -0500 (CDT), Ed Cavazos wrote:
> However, up to this point my program components
> have been a collection of function definitions, and sometimes, global
> variables which can be used to tweak the behaviour of the
> functions. Well, it seems the module paradigm is incompatible with
> this style of component. I'm afraid that the recommended solution is
> going to be to wrap the variables with get and set functions, and
> export those...
We use parameters for this kind of task. It's hadly any extra work at
the definition site:
(define foo-level (make-parameter 5))
; instead of
(define foo-level 5)
or at the use site:
(foo-level (add1 (foo-level)))
; instead of
(set! foo-level (add1 foo-level))
There are other advantages to parameters, besides working with modules:
you can easily attach a guard to the parameter to check assignments
(helpful in debugging), parameters work better with threads, and you
can abstract easily over configuration "variables" (because parameters
are first-class values).
Matthew