4.9 More About Global Variables

The vast majority of variables in GAP are defined at the outer level (the global scope). They are used to access functions and other objects created either in the GAP library or in the user's code. Certain special facilities are provided for manipulating these variables which are not available for other types of variable (such as local variables or function arguments).

First, such variables may be marked read-only. In which case attempts to change them will fail. Most of the global variables defined in the GAP library are so marked.

  • IsReadOnlyGlobal ( name ) F

    IsReadOnlyGlobal ( name ) returns true if the global variable named by the string name is read-only and false otherwise (the default).

  • MakeReadOnlyGlobal ( name ) F

    MakeReadOnlyGlobal ( name ) marks the global variable named by the string name as read-only.

    A warning is given if name has no value bound to it or if it is already read-only

  • MakeReadWriteGlobal ( name ) F

    MakeReadWriteGlobal ( name ) marks the global variable named by the string name as read-write

    A warning is given if name is already read-write

    gap> xx := 17;
    17
    gap> IsReadOnlyGlobal("xx");
    false
    gap> xx := 15;
    15
    gap> MakeReadOnlyGlobal("xx");
    gap> xx := 16;
    Variable: 'xx' is read only
    not in any function
    Entering break read-eval-print loop, you can 'quit;' to quit to outer loop,
    or you can return after making it writable to continue
    brk> quit;
    gap> IsReadOnlyGlobal("xx");
    true
    gap> MakeReadWriteGlobal("xx");
    gap> xx := 16;
    16
    gap> IsReadOnlyGlobal("xx");   
    false
    

    A group of functions are also supplied for accessing and altering the values assigned to global variables. Use of these functions differs from the use of assignment, Unbind and IsBound statements, in two ways. First, these functions always affect global variables, even if local variables of the same names exist. Second, the variable names are passed as strings, rather than being written directly into the statements.

  • ValueGlobal ( name ) F

    ValueGlobal ( name ) returns the value currently bound to the global variable named by the string name. An error is raised if no value is currently bound.

  • IsBoundGlobal ( name ) F

    IsBoundGlobal ( name ) returns true if a value currently bound to the global variable named by the string name and false otherwise.

  • UnbindGlobal ( name ) F

    UnbindGlobal ( name ) removes any value currently bound to the global variable named by the string name. Nothing is returned.

    A warning is given if name was not bound. The global variable named by name must be writable, otherwise an error is raised.

  • BindGlobal ( name, val ) F

    BindGlobal ( name, val ) sets the global variable named by the string name to the value val, provided it is writable, and makes it read-only. If name already has a value, a warning message is printed.

    This is intended to be the normal way to create and set ``official'' global variables (such as Operations and Categories).

    Caution should be exercised in using these functions, especially BindGlobal and UnbindGlobal as unexpected changes in global variables can be very confusing for the user.

    gap> xx := 16;
    16
    gap> IsReadOnlyGlobal("xx");   
    false
    gap> ValueGlobal("xx");
    16
    gap> IsBoundGlobal("xx");
    true
    gap> BindGlobal("xx",17);
    #W BIND_GLOBAL: variable `xx' already has a value
    gap> xx;
    17
    gap> IsReadOnlyGlobal("xx");
    true
    

    Finally, there are a group of functions dealing with the global namespace.

  • NamesGVars() F

    This function returns an immutable (see Mutability and Copyability) sorted (see Sorted Lists and Sets) list of all the global variable names known to the system. This includes names of variables which were bound but have now been unbound and some other names which have never been bound but have become known to the system by various routes.

  • NamesSystemGVars() F

    This function returns an immutable sorted list of all the global variable names created by the GAP library when GAP was started.

  • NamesUserGVars() F

    This function returns an immutable sorted list of the global variable names created since the library was read, to which a value is currently bound.

  • TemporaryGlobalVarName( [prefix] ) F

    TemporaryGlobalVarName ( [prefix] ) returns a string that can be used as the name of a global variable that is not bound at the time when TemporaryGlobalVarName() is called. The optional argument prefix can specify a string with which the name of the global variable starts.

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000