Node:Variables, Previous:Dynamic Libraries, Up:Modules

31.5 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 relationship is mutual: the variable is bound to the name (in that module), and the name (in that module) is bound to the variable.

(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.

First-class variables are especially useful for interacting with the current module system (see The Guile module system).

make-undefined-variable Scheme Procedure
scm_make_undefined_variable () C Function
Return a variable that is initially unbound.

make-variable init Scheme Procedure
scm_make_variable (init) C Function
Return a variable initialized to value init.

variable-bound? var Scheme Procedure
scm_variable_bound_p (var) C Function
Return #t iff var is bound to a value. Throws an error if var is not a variable object.

variable-ref var Scheme Procedure
scm_variable_ref (var) C Function
Dereference var and return its value. var must be a variable object; see make-variable and make-undefined-variable.

variable-set! var val Scheme Procedure
scm_variable_set_x (var, val) C Function
Set the value of the variable var to val. var must be a variable object, val can be any value. Return an unspecified value.

variable? obj Scheme Procedure
scm_variable_p (obj) C Function
Return #t iff obj is a variable object, else return #f.