Type equality
The meaning of basic operations, such as assignment (denoted as = in C), is specified in the language definition. For example, the meaning of statements such as
x = y;
here the value of the object y copied to the memory cells for the variable x .
However, before an operation, such as an assignment, can be accepted by the translator, typically the types of the two operands must be the same (or possibly compatible in some other way).
Thus, the language translator must decide whether the two types are equal in some cases. Now we look at what it means to say that two types are "equal" (or equivalent).
There are two standard ways to determine if two types are considered the same: name equivalence and structural equivalence .
Equivalence of names is the simplest: two types are equal if and only if they have the same name. Thus, for example, in code (using C syntax)
typedef struct { int data[100]; int count; } Stack; typedef struct { int data[100]; int count; } Set; Stack x, y; Set r, s;
if equivalence of names is used in the language, then x and y will be of the same type, and r and s will be of the same type, but the type of x or y will not be equivalent to the type of r or s . This means statements like
x = y; r = s;
will be valid but expressions like
x = r;
invalid (i.e. will not be accepted by the translator).
Using structural equivalence:, two types are equal if and only if they have the same โstructureโ, which can be interpreted differently. A rigorous interpretation will be that the names and types of each component of the two types must be the same and must be listed in the same order in the type definition. A less stringent requirement would be that the types of the components must be the same and in the same order in the two types, but the names of the components may be different.
Having considered the above example again, using structural equivalence , the two types Stack and Set will be considered equivalent, which means that the translator accepts statements such as
x = r;
(Note that C does not support structural equivalence and will give an error for the above purpose.)