[Oberon] Case statements containing base type labels

joerg.straube at iaeth.ch joerg.straube at iaeth.ch
Mon Aug 28 19:10:20 CEST 2023


August

You are right: by only reading the Oberon-07 report
https://people.inf.ethz.ch/wirth/Oberon/Oberon07.Report.pdf
without having the background info given in chapter 23.4
https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf
you don’t know that the “CASE with types” must be seen as an IF-ELSE cascade (implying that the order is important!).

This code snippet
   CASE animal OF
      Squirrel: Out.String(”Squirrel”)|
      Whale: Out.String(“Whale”) |
      Mammal: Out.String(“Mammal”)
    END;

and this snippet
   CASE animal OF
      Mammal: Out.String(“Mammal”) |
      Squirrel: Out.String(”Squirrel”) |
      Whale: Out.String(“Whale”)
   END;

is NOT the same.

br
Jörg

Von: Oberon <oberon-bounces at lists.inf.ethz.ch> im Auftrag von joerg.straube at iaeth.ch <joerg.straube at iaeth.ch>
Datum: Montag, 28. August 2023 um 18:50
An: ETH Oberon and related systems <oberon at lists.inf.ethz.ch>
Betreff: Re: [Oberon] Case statements containing base type labels
August

As Chris said, the CASE is ”just” a cascade of IF-ELSE.
Assume you have
TYPE
  Animal = RECORD age: INTEGER END
  Mammal = RECORD (Animal) laysEggs: BOOLEAN END;
  Squirrel = RECORD (Mammal) hiddenNuts: INTEGER END;
  Whale = RECORD (Mammal) weight: INTEGER END;


PROCEDURE Feed(animal: Animal)
  BEGIN
   CASE animal OF
      Squirrel: Out.String(”nuts for winter:”) Out.Int(animal.hiddenNuts, 0)|
      Whale: Out.String(“weight in tons:”); Out.Int(animal.weight) |
      Mammal: Out.String(“Mammal”)
    END;
    Out.Ln
  END Feed;

VAR s: Squirrel; s.hiddenNuts := 10; Feed(s); (* writes out the nbr of nuts *)
VAR w: Whale; s.weight := 330; Feed(w); (* writes out its weight *)
VAR m: Mammal; Feed(m); (* writes “Mammal” *)

Let’s assume, with Feed() as given above you define
TYPE
  Platypus = RECORD (Mammal) livesInAustralia: BOOLEAN END;
  Bird = RECORD (Animal) nbrOfFeathers: INTEGER END;
VAR p: Platypus; Feed(p); (* writes “ Mammal” as p is a Mammal *)
VAR b: Bird; b.nbrOfFeathers := 2500; Feed(b); (* writes nothing as b is neither a Squirrel nor a Whale nor a Mammal *)

br
Jörg

Von: Oberon <oberon-bounces at lists.inf.ethz.ch> im Auftrag von Chris Burrows <cfbsoftware at gmail.com>
Datum: Montag, 28. August 2023 um 15:37
An: ETH Oberon and related systems <oberon at lists.inf.ethz.ch>
Betreff: Re: [Oberon] Case statements containing base type labels
On Mon, Aug 28, 2023 at 10:00 PM August Karlstrom <fusionfile at gmail.com<mailto:fusionfile at gmail.com>> wrote:

However, the same dilemma occurs also if we consider strict extensions,
for example

        CASE animal OF
                Squirrel: ... |
                Whale: ... |
                Mammal: ...
        END


This is just equivalent to (and makes as much sense as):

   IF animal IS Squirrel THEN ...
   ELSIF animal IS Whale THEN ...
   ELSIF animal is Mammal THEN ...
   END

Refer to the pair of examples on P 61 of  Programming in Oberon, 2014:

https://people.inf.ethz.ch/wirth/Oberon/PIO.pdf

Regards,
Chris

--
Chris Burrows
CFB Software
https://www.astrobe.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20230828/9b66fa13/attachment-0001.html>


More information about the Oberon mailing list