[Oberon] Clarifying type compatibility in Oberon-07
andreas_pirklbauer at yahoo.com
Thu Oct 12 14:16:22 CEST 2017
> Le 12/10/2017 à 01:51, Stéphane Aulery a écrit:
> M and M2 are valid but M1, M3, M4 and M5 are invalid, so it seems inconsistent in fact. No?
Unfortunately it is a little unclear what the current Oberon-07 language *specification* available
at www.inf.ethz.ch/personal/wirth <http://www.inf.ethz.ch/personal/wirth> implies when it comes to assignments. In ch. 9.1. of the language
report, the assignment rules for an assignment "designator := expression” are defined as follows:
"The type of the expression must be the SAME as that of the designator. The following exceptions hold:
1. The constant NIL can be assigned to variables of any pointer or procedure type.
2. Strings can be assigned to any array of characters, provided the number of
characters in the string is less than that of the array. (A null character is
appended). Single-character strings can also be assigned to variables of type CHAR.
3. In the case of records, the type of the source must be an extension of the type of
the destination. 4. An open array may be assigned to an array of equal base type.”
while in ch. 6.5 of the report the assignment rules for procedures are specified as follows:
"If a procedure P is assigned to a procedure variable of type T, the (types of the) formal
parameters of P must be the same as those indicated in the formal parameters of T. The same
holds for the result type in the case of a function procedure."
However, the above rules do not actually specify what exactly is meant by the “same" type.
But the *current* implementation of the Oberon-07 compiler, which is also available at
www.inf.ethz.ch/personal/wirth <http://www.inf.ethz.ch/personal/wirth> implements the following rules for *same* types:
Two variables *a* and *b* with types *Ta* and *Tb* are of the *same type* if
1. *Ta* and *Tb* are both denoted by the same type identifier, or
2. *Ta* is declared to equal *Tb* in a type declaration of the form *Ta* = *Tb*, or
3. *a* and *b* appear in the same identifier list in a variable, record field, or
formal parameter declaration and are not open arrays, or
4. *Ta* and *Tb* are array types with same element types and length.
Thus, for array and procedures *structural equivalence* suffices, whereas for records
structural equivalence does not suffice (in the *current* implementation of the
compiler), as can easily be seen by compiling the following small test program:
MODULE M; (*test of the implementation of Oberon-07 as of 12.10.17*)
p, q: ARRAY 10 OF INTEGER;
r, s: ARRAY 10 OF INTEGER;
e, f: PROCEDURE (i, j: INTEGER);
g, h: PROCEDURE (i, j: INTEGER);
a, b: RECORD i, j: INTEGER END ;
c, d: RECORD i, j: INTEGER END ;
p := q; (*ok -> same array types*)
p := r; (*ok -> structural equivalence suffices*)
e := f; (*ok -> same procedure types*)
e := g; (*ok -> structural equivalence suffices*)
a := b; (*ok -> same record types*)
a := c (*illegal assignment -> no structural equivalence for records*)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Oberon