Next: , Previous: , Up: Modules   [Contents][Index]


6.20.7 Variables

Each module has its own hash table, sometimes known as an obarray, that maps the names defined in that module to their corresponding variable objects.

A variable is a box-like object that can hold any Scheme value. It is said to be undefined if its box holds a special Scheme value that denotes undefined-ness (which is different from all other Scheme values, including for example #f); otherwise the variable is defined.

On its own, a variable object is anonymous. A variable is said to be bound when it is associated with a name in some way, usually a symbol in a module obarray. When this happens, the name is said to be bound to the variable, in that module.

(That’s the theory, anyway. In practice, defined-ness and bound-ness sometimes get confused, because Lisp and Scheme implementations have often conflated — or deliberately drawn no distinction between — a name that is unbound and a name that is bound to a variable whose value is undefined. We will try to be clear about the difference and explain any confusion where it is unavoidable.)

Variables do not have a read syntax. Most commonly they are created and bound implicitly by define expressions: a top-level define expression of the form

(define name value)

creates a variable with initial value value and binds it to the name name in the current module. But they can also be created dynamically by calling one of the constructor procedures make-variable and make-undefined-variable.

Scheme Procedure: make-undefined-variable
C Function: scm_make_undefined_variable ()

Return a variable that is initially unbound.

Scheme Procedure: make-variable init
C Function: scm_make_variable (init)

Return a variable initialized to value init.

Scheme Procedure: variable-bound? var
C Function: scm_variable_bound_p (var)

Return #t if var is bound to a value, or #f otherwise. Throws an error if var is not a variable object.

Scheme Procedure: variable-ref var
C Function: scm_variable_ref (var)

Dereference var and return its value. var must be a variable object; see make-variable and make-undefined-variable.

Scheme Procedure: variable-set! var val
C Function: scm_variable_set_x (var, val)

Set the value of the variable var to val. var must be a variable object, val can be any value. Return an unspecified value.

Scheme Procedure: variable-unset! var
C Function: scm_variable_unset_x (var)

Unset the value of the variable var, leaving var unbound.

Scheme Procedure: variable? obj
C Function: scm_variable_p (obj)

Return #t if obj is a variable object, else return #f.


Next: , Previous: , Up: Modules   [Contents][Index]