3.3. Global type declarations.   

Many languages (for example Pascal) are very restrictive on the declaring of variables of a specific type from two different parts of a program. E.g:


var // GLOBAL DECLARATION.
x:array [1..35] of integer;
:
procedure xxxxxx
var // LOCAL DECLARATION.
y:array [1..35] of integer;

It may be a compiler error to make the assignment x y;
even though they are obviously of the same type.

Even worse is the following:


var // GLOBAL DECLARATION.
x:array [1..35] of integer;
y:array [1..35] of integer;

Some languages may still not allow an assignment from x to y ( or vice versa). This is because they are not exactly the same type. To make them exact you must use:

type fish=array [1..35] of integer;
var x:fish;
y:fish;
or
var x,y:array [1..35] of integer;

In other words, they must be of the same simple type name or be declared as the same type in the same declaration! This applies to any non-simple type not just arrays.

Why? This is because the compiler must determine if the variables are of a compatible type. When it is processing the variable declarations, it creates symbol table information about the type of the variable, puts it into a record and then points the variable's "what is my type" pointer to the record. When it checks for compatibility, it simply checks to see if the two pointers are the same not if they point to things which are the same. Variables declared in the same declaration or with the same simple type, will have identical pointers. Variables declared in different declarations will have different pointers (even though they may point to records which are the same). This is the difference between name equivalence and structure equivalence.

It is often more convenient to produce a new type (in this case type fish) in case other variables or, more importantly, procedure parameters are of that type.

In some languages, it is illegal to declare a procedure as follows:


procedure sss(a:array[1..5] of ...);

The type of a procedure parameter must be a simple type: that is it must be a predefined type or have been defined in a users type declaration.

procedure sss(a:fish);

The "what is my type" pointer for the first example above must be different from any other "compatible" declaration and hence you could never pass a compatible parameter.