5. Embedded QUEL for Ada : Ada Variables and Data Types : Compilation Units and the Scope of Variables : Variable and Type Scope
 
Share this page                  
Variable and Type Scope
As mentioned above, variables and types are visible in the block in which they are declared, unless they are declared in a package specification, in which case they are globally visible. Variables can be redeclared only in a nested scope, such as in a declaration block or a nested procedure. Variables cannot be redeclared in the same scope. For example, the following two enumerated type declarations in the same scope will cause a redeclaration of the overloaded literal "UNDEFINED":
## type Question is (SIMPLE, DIFFICULT, UNDEFINED);
## type Answer is (WRONG, RIGHT, SORT_OF, UNDEFINED);
Note that you can declare record components with the same name but different record types. The following example declares two records, each of which has the components "firstname" and "lastname":
## type Child is
## record
##     firstname: String(1..15);
##     lastname: String(1..20);
##     age: Integer;
## end record;

## type Some_Childs is array(1..10) of Child;

## type Mother is
## record
##     firstname: String(1..15);
##     lastname: String(1..20);
##     num_child: Integer range 1..10;
##     children: Some_Childs;
## end record;
The following example shows several different declarations of the variable "var," illustrating how the same object can be redeclared in nested and parallel scopes, each time referring to a different type:
## with equel;

## procedure Proc_A(var: type_1) is

-- Will be used even when this particular "var" is hidden
## proc_a_var: type_1 renames var;

## procedure Proc_B is
##   var: type_2;
## begin   
   -- Var is of type_2
## end Proc_B;
## function Func_C(var: type_3) return Integer is
## begin

  -- Var is of type_3
  -- Note that you cannot refer to Proc_A.var
  -- but you can refer to proc_a_var of type_1.
## end Func_C;

## begin
     -- Var is of type_1

## declare
## var: type_4;
## begin
   -- Var is of type_4;
## end;

-- Var is of type_1

## end Proc_A;
Take special care when using variables with a declare cursor statement. The variables used in such a statement must also be valid in the scope of the open statement for that same cursor. The preprocessor actually generates the code for the declare at the point that the open is issued, and at that time, evaluates any associated variables. For example, in the following program fragment, even though the variable "number" is valid to the preprocessor at the point of the declare cursor statement, it is not a valid variable name for the Ada compiler at the point that the open is issued.
## package Bad_Cursor is 
        --This example contains an error

## procedure Init_Csr is
## number: Integer;
## begin

-- Cursor declaration includes reference to "number"
## declare cursor c1 for
##    retrieve (employee.name, employee.age)
##      where employee.num = number

...

## end Init_Csr;
## procedure Process_Csr is
## ename: String(1..15);
## eage: Integer;
## begin
   -- Opening the cursor evaluates invalid "number"
## open cursor c1

## retrieve cursor c1 (ename, eage)

...

## end Process_Csr;

## end Bad_Cursor;