www.delorie.com/gnu/docs/guile/guile_364.html   search  
Buy GNU books!

Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 section 31.3 The Guile module system).

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 iff var is bound to a value. 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? obj
C Function: scm_variable_p (obj)
Return #t iff obj is a variable object, else return #f.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003