[Oberon] Case statements containing base type labels

joerg.straube at iaeth.ch joerg.straube at iaeth.ch
Mon Aug 28 18:49:47 CEST 2023


As Chris said, the CASE is ”just” a cascade of IF-ELSE.
Assume you have
  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)
   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 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
  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 *)


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: ...

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 ...

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



Chris Burrows
CFB Software

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

More information about the Oberon mailing list