Like all languages in the Algol family, Modula-3 is strongly typed. The basic
idea of strong typing is to partition the value space into types, restrict
variables to hold values of a single type, and restrict operations to apply to
operands of fixed types. In actuality, strong typing is rarely so simple.
For example, each of the following complications is present in at least one
language of the Algol family: a variable of type [0..9]
may be safely
assigned to an INTEGER
, but not vice-versa (subtyping). Operations
like absolute value may apply both to REAL
s and to INTEGER
s
instead of to a single type (overloading). The types of literals (for
example, NIL
) can be ambiguous. The type of an expression may be
determined by how it is used (target-typing). Type mismatches may cause
automatic conversions instead of errors (as when a fractional real is rounded
upon assignment to an integer).
We adopted several principles in order to make Modula-3's type system as uniform as possible. First, there are no ambiguous types or target-typing: the type of every expression is determined by its subexpressions, not by its use. Second, there are no automatic conversions. In some cases the representation of a value changes when it is assigned (for example, when assigning to a packed field of a record type) but the abstract value itself is transferred without change. Third, the rules for type compatibility are defined in terms of a single subtype relation. The subtype relation is required for treating objects with inheritance, but it is also useful for defining the type compatibility rules for conventional types.