Java Reference
In-Depth Information
it will return a reference to a special TypeDescriptor that we will refer to as
errorType . That particular value will be a signal to the code that initiated
semantic processing that an error has been detected and that an error message
has already been generated. In this calling context, an explicit check may be
made for errorType or it may be treated like any other TypeDescriptor.Inthe
specific instance of the variable declaration pseudocode, the possibility of an
errorType return can be ignored. It works perfectly well to declare variables
with errorType as their type. In fact, doing so prevents later, unnecessary error
messages. That is, if a variable is left undeclared because of a type error in
its declaration, each time it is used, the compiler will generate an undeclared
variable error message. In later sections, we will see other uses of errorType to
avoid the generation of extraneous error messages.
Type Compatibility
One question remains to be answered: Just what does it mean for types to be
the same or for a constraint (as used in Ada) to be compatible with a type?
Ada and Pascal have a strict definition of type equivalence that says that every
type definition defines a new, distinct type that is incompatible with all other
types. This definition means that the declarations
A, B : ARRAY (1..10) OF Integer;
C, D : ARRAY (1..10) OF Integer;
are equivalent to
type Type1 is ARRAY (1..10) OF Integer;
A, B : Type1;
type Type2 is ARRAY (1..10) OF Integer;
C, D : Type2;
A and B are of the same type and C and D are of the same type. However, the
two types are defined by distinct type definitions and thus are incompatible.
As a result, assignment of the value of C to A would be illegal. This rule is
easily enforced by a compiler. Since every type definition generates a distinct
type descriptor, the test for type equivalence requires only a comparison of
pointers.
Other languages, most notably C, C
erent rules
to define type equivalence. The most common alternative is to use structural
type equivalence . As the name suggests, two types are equivalent under
this rule if they have the same definitional structure. Thus Type1 and Type2
from the previous example would be considered equivalent. At first glance,
this rule appears to be a much more useful choice because it seems more
convenient for programmers using the language. However, counterbalancing
++
and Algol 68, use di
ff
 
Search WWH ::




Custom Search