Concepts of Programming Languages

(Sean Pound) #1

308 Chapter 6 Data Types


Name type equivalence works well for Ada, in part because all types, except
anonymous arrays, are required to have type names (and anonymous types are
given internal names by the compiler).
Type equivalence rules for Ada are more rigid than those for languages
that have many coercions among types. For example, the two operands of an
addition operator in Java can have virtually any combination of numeric types
in the language. One of the operands will simply be coerced to the type of
the other. But in Ada, there are no coercions of the operands of an arithmetic
operator.
C uses both name and structure type equivalence. Every struct, enum,
and union declaration creates a new type that is not equivalent to any other
type. So, name type equivalence is used for structure, enumeration, and union
types. Other nonscalar types use structure type equivalence. Array types are
equivalent if they have the same type components. Also, if an array type has a
constant size, it is equivalent either to other arrays with the same constant size
or to with those without a constant size. Note that typedef in C and C++ does
not introduce a new type; it simply defines a new name for an existing type.
So, any type defined with typedef is type equivalent to its parent type. One
exception to C using name type equivalence for structures, enumerations, and
unions is if two structures, enumerations, or unions are defined in different
files, in which case structural type equivalence is used. This is a loophole in the
name type equivalence rule to allow equivalence of structures, enumerations,
and unions that are defined in different files.
C++ is like C except there is no exception for structures and unions defined
in different files.
In languages that do not allow users to define and name types, such as
Fortran and COBOL, name equivalence obviously cannot be used.
Object-oriented languages such as Java and C++ bring another kind of type
compatibility issue with them. The issue is object compatibility and its relation-
ship to the inheritance hierarchy, which is discussed in Chapter 12.
Type compatibility in expressions is discussed in Chapter 7; type compat-
ibility for subprogram parameters is discussed in Chapter 9.

6.15 Theory and Data Types


Type theory is a broad area of study in mathematics, logic, computer science,
and philosophy. It began in mathematics in the early 1900s and later became
a standard tool in logic. Any general discussion of type theory is necessarily
complex, lengthy, and highly abstract. Even when restricted to computer sci-
ence, type theory includes such diverse and complex subjects as typed lambda
calculus, combinators, the metatheory of bounded quantification, existential
types, and higher-order polymorphism. All of these topics are far beyond the
scope of this book.
In computer science there are two branches of type theory: practical and
abstract. The practical branch is concerned with data types in commercial
Free download pdf