[Oberon] Clarifying type compatibility in Oberon-07
Diego Sardina
dsar at eml.cc
Thu Oct 12 07:08:46 CEST 2017
On Thu, Oct 12, 2017, at 01:51 AM, Stéphane Aulery wrote:
> -----------------------------------------------------------------------
> MODULE M2;
> TYPE
> Tb = RECORD
> aa: INTEGER;
> bb: ARRAY 10 OF INTEGER;
> END;
> Te = RECORD (Tb)
> dd: INTEGER;
> END;
>
> VAR
> x: Tb;
> y: Te;
> BEGIN
> x := y;
> END M2.
> -----------------------------------------------------------------------
> MODULE M3;
> TYPE
> Tb = RECORD
> aa: INTEGER;
> bb: ARRAY 10 OF INTEGER;
> END;
> Te = RECORD (Tb)
> dd: INTEGER;
> END;
>
> VAR
> x: Tb;
> y: Te;
> BEGIN
> y := x;
> END M3.
> -----------------------------------------------------------------------
>
> Wy M3 not allowed? I can loose data with M2 in assignment but I can't
> assign a base type variable to an extended type variable.
>
Procedures that work on a base type are not aware of additional fields,
then you don't loose data in the case of M2.
Furthermore, they have to work on that type and not for its extensions.
TYPE
Person = RECORD firstname, lastname: ARRAY 32 OF CHAR END;
Worker = RECORD (Person) job: ARRAY 32 OF CHAR END;
VAR
p1: Person;
p2: Worker;
A procedure CivilOffice.Registry(p2) that accepts a Person by value, is
not interested in fields that are part of its extensions. In this
procedure I'm only interested to save first and last name, not his job
(or whatever fields have its extensions).
On the contrary, assignment of a base type to an extended one is not
typesafe because it causes *inconsistent data*.
p1.firstname := "Diego";
p1.lastname := "Sardina";
p2.firstname := "Freddy";
p2.lastname := "Krueger";
p2.job := "Serial killer";
p2 := p1; (* presuming it was possible *)
p2 would be
p2.firstname = "Diego";
p2.lastname = "Sardina";
p2.job = "Serial killer";
With p2 := p1, I became a serial killer! :-)
--
Diego Sardina
More information about the Oberon
mailing list