[Oberon] Dynamic type of record var parameter

Chris Burrows chris at cfbsoftware.com
Tue Jan 12 09:37:58 CET 2021


> -----Original Message-----
> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of
> chris
> Sent: Tuesday, 12 January 2021 9:14 AM
> To: oberon at lists.inf.ethz.ch
> Subject: [Oberon] Dynamic type of record var parameter
> 
> Hello,
> 
> please consider the following test of dynamic type of record variable
> parameters:
> 
> MODULE Test;
> 
>   IMPORT Texts, Oberon;
> 
> TYPE
> 	r0 = RECORD i : INTEGER; END;
> 	r1 = RECORD (r0) x : REAL END;
> 	r2 = RECORD (r1) c : CHAR END;
> 	p0 = POINTER TO r0;
> 	p1 = POINTER TO r1;
> 	p2 = POINTER TO r2;
> 
> VAR W : Texts.Writer;
> 
> PROCEDURE check(VAR r : r0);
> BEGIN
> 	IF r IS r2 THEN Texts.WriteString(W, "is r2") ELSIF r IS r1
> THEN
> 		Texts.WriteString(W, "is r1")
> 	ELSIF r IS r0 THEN Texts.WriteString(W, "is r0") END;
> 	Texts.WriteLn(W)
> END eins;
> 
> PROCEDURE do* ();
> VAR p : p0;  r : r0; q : p2; t : r2;
> BEGIN
> 	NEW(p); NEW(q);
> 	check(t);
> 	check(p^);
> 	check(q^);
> 	p := q;  check(p^);
> 	Texts.Append(Oberon.Log, W.buf);
> END do;
> 
> BEGIN
> 	Texts.OpenWriter(W);
> END Test.
> 
> Output from Oberon 2013 emulator is:
> is r2
> is r0
> is r2
> is r0
> 
> What  surprises me is the last one. The dynamic type is r2 not r0. It
> looks like the compiler uses the static type from the local variable
> p instead of the dynamic type of p^. Is this intended behavior?
> 

My understanding is that you need to use a type guard to assert that p can
be assumed to have a dynamic type of p2:

i.e. p := q; check(p(p2)^);

or to be safe:

p := q; IF p IS p2 THEN check(p(p2)^) END;

However, I'm not a fan of anonymous variables so I would be more likely to
write:

p := q; IF p IS p2 THEN t := p(p2)^; check(t) END;

For more information refer to Section 11.2 in Programming in Oberon by
Martin Reiser and Niklaus Wirth:

https://people.inf.ethz.ch/wirth/ProgInOberonWR.pdf

Regards,
Chris Burrows
CFB Software
https://www.astrobe.com/RISC5








More information about the Oberon mailing list