Expressions and implicit conversions AI-00165/04 1
88-11-04 co WA
| !standard 04.06 (15) 88-11-04 AI-00165/04
!class confirmation 87-07-14
| !status approved by WG9 88-10-28
!status ARG-approved 88-05-10 (reviewed)
!status ARG-approved (15-0-0) 87-09-15 (pending editorial review)
!status panel-approved (6-0-0) 87-09-15
!status work-item 87-07-13
!status received 84-01-13
!references 83-00249
!topic Expressions and implicit conversions
!summary 88-04-13
Only numeric literals, named numbers, and attributes can be converted
implicitly to a numeric type.
!question 88-04-13
4.10(3) says that the following predefined operators exist:
"*" (universal_integer, universal_real) --> universal_real
"/" (universal_real, universal_integer) --> universal_real
The existence of these operators means 100 * 100.0 and 10.0/2 are legal
expressions in contexts that allow or require a universal_real result, e.g.,
in the initialization expression for a named number. Can such expressions be
used in other contexts as well? For example, consider:
X, Y : FLOAT := ...;
R : constant := 10.0/2; -- (1) legal
...
X := 100 * 100.0 * Y; -- (2) legal? (no)
Y := 10.0/2; -- (3) legal? (no)
The assignments to X and Y should be considered legal if 100 * 100.0 and
10.0/2 return universal_real values that can be converted implicitly to
FLOAT. Are such implicit conversions allowed?
!response 88-04-13
Only numeric literals, named numbers, and attributes can be converted
implicitly to a numeric type. 4.6(15) gives the rule:
Apart from the explicit type conversions, the only allowed form
of type conversion is the implicit conversion of a value of the
type universal_integer or universal_real into another numeric
type. An implicit conversion of an operand of type universal_
integer to another integer type, or of an operand of type
universal_real to another real type, can only be applied if the
operand is either a numeric literal, a named number, or an
attribute; such an operand is called a convertible universal
operand in this section. An implicit conversion of a convertible
universal operand is applied if and only if the innermost
Expressions and implicit conversions AI-00165/04 2
88-11-04 co WA
complete context (see 8.7) determines a unique (numeric) target
type for the implicit conversion, and there is no legal
interpretation of this context without this conversion.
The second sentence of 4.6(15) only allows implicit conversions to be applied
to numeric literals, named numbers, and attributes, i.e., not to expressions
such as 10.0/2 that contain an operator. Since neither the literal 2 nor the
result of 10.0/2 can be converted implicitly to FLOAT, the assignment to Y in
the question is illegal. Similarly, 100 * 100.0 * Y is only legal if the
result of 100 * 100.0 can be converted implicitly to FLOAT, and 4.6(15) does
not allow such an implicit conversion. The programmer should have written:
X := 100.0 * 100.0 * Y;
Y := 100.0/2.0;
Extensive consideration was given to the treatment of implicit conversions
during the language design. Examples such as the ones given in the question
were considered, as were several alternative implicit conversion rules. In
the end, it was considered simpler and sufficient if implicit conversions
were only allowed for literals, named numbers, and attributes that return
values of a universal type.