HP Pascal/iX Reference Manual (31502-90022)

11- 16
Syntactically, value type coercion is allowed:
* In an expression.
* On the right-hand side of an assignment statement.
* On an actual parameter.
By default, the compiler does not allow value type coercion. The
compiler option TYPE_COERCION allows the user to enable a certain level
of type coercion. There are three classes of type coercion based on the
source and target types: ordinal, pointer, and free union type coercion.
Ordinal and pointer type coercions are enabled by specifying the
conversion level of type coercion. Instances of free union type coercion
are enabled by specifying one of
structural
,
representation
,
storage
, or
noncompatible
type coercion.
Ordinal Type Coercion.
The
ordinal
types are viewed as different sets of names for the points on
the integer number line. Given this view of ordinals, value type
coercion of one ordinal type to another is simply a renaming operation.
A type coercion expression is considered an
ordinal coercion
, if both the
source expression (expression being coerced) and the target type (type to
which the expression is being coerced) are any of the following types:
* The predefined types integer, shortint, char, Boolean, and Bit16.
* A user-declared enumerated type.
* A user-declared subrange type.
If the value of the source expression is out of range with respect to the
allowed values of the target type, a subrange violation occurs. If range
checking is on, this causes a run-time error.
Example
TYPE
color_t = (red,orange,yellow,green,chartreuse,blue,indigo,violet);
VAR
i : integer;
color : color_t;
BEGIN
...
color := chartreuse;
i := integer( color ); { i has the value 4 }
i := 3;
color := color_t( i ); { color has the value green }
i := 12;
color := color_t( i ); { will cause a run-time error }
...
END;
Pointer Type Coercion.
The pointer types are viewed as virtual addresses. Given this view,
value type coercion from one pointer type to another is a mapping from
one virtual address to another.
On implementations that have alignment restrictions, it is an error if
the alignment of the type that the source expression points to is smaller
than the alignment of the type that the target type points to. If range
checking is on, this causes a run-time error. See the
HP Pascal/iX
Programmer's Guide
or the
HP Pascal/HP-UX Programmer's Guide
, depending
on your implementation, for more information on alignment.
Coercion from long-to-long and short-to-short pointers is a one-to-one
mapping and involves no actual run-time conversion operations.
Coercion from a short to a long pointer, in implementations where long
pointers point to a wider class of objects than short pointers, may
involve amending the value of the short pointer with additional address