[Oberon] Type compatibility rules for Pointers

Michael Schierl schierlm at gmx.de
Mon Jun 15 21:42:39 CEST 2020


Hello Chris,


Am 15.06.2020 um 21:09 schrieb chris:

> TYPE
> 	R = RECORD i : INTEGER END;
> 	P1 = POINTER TO R;
> 	P2 = POINTER TO R;
>
> PROCEDURE Test();
> VAR
> 	p1 : P1; p2 : P2;
> BEGIN
> 	NEW(p1); NEW(p2);
> 	IF p1 IS P2 THEN (* true *) END;
> 	p1 := p2;
> END Test;
>
> The dynamic type test is not a surprise as the type tag refers to the
> same record descriptor although I think it's a bit counter intuitive.

Are you equally confused when you replace POINTER TO R by ARRAY 42 OF
BYTE or even by INTEGER?

i.e.

TYPE I1 = INTEGER; I2 = INTEGER;
  A1 = ARRAY 42 OF BYTE;
  A2 = ARRAY 42 OF BYTE;


You can assign variables of type I1 to variables of type I2 or variables
of type INTEGER and vice versa. Same with the array types.

> But in my understanding pointers are not records and P1 and P2 are
> different types.
>
> Any comments?

As I understand it, of all custom types only record types have identity.
All other types are just aliases of what is on the other side of the
equals sign. So it should not matter if you replace every occurrence of
I1 by INTEGER or A1 by ARRAY 42 OF BYTE (except that the Oberon-07
compiler does not like unnamed non-open arrays as procedure parameter
types).

Records behave differently, RECORD i: INTEGER END cannot be replaced by
R as each record type has identity.

I am not sure where I have this from (I can see that it is not actually
defined in the Oberon 07 Report), perhaps this is just from experience
from other programming languages.


Regards,


Michael


More information about the Oberon mailing list