🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #1040

Re: Implementing Abstract Lists

#1040
From: Pat Terry
Date: Sun, 05 Aug 1990 05:14
73 lines
2804 bytes
dgil@pa.reuter.COM (Dave Gillett) in <290@saxony.pa.reuter.COM> writes

 > So I was severely disappointed to discover that the "List" module 
 > does not in fact deal with lists in the abstract, but with lists of things 
 > of type "ItemType" which is imported from some other module.  So in a 
 > typical real-world program, if I have three types of things that I want 
 > lists of, I need three (almost but not quite identical) copies of the 
 > List module as presented.

I haven't got my copy of Harrison handy.  It's not true; you can use one copy 
if you are careful.  It's slightly messy, I concede.

    DEF MOD  OneHandler;
      TYPE One;
      (* all operations on One in fairly stereotyped form *)
    END OneHandler.

    DEF MOD TwoHandler;
      TYPE Two;
      (* all operations on Two ditto *)
    END TwoHandler.

Now these are opaque types.  So they are really addresses anyway.  So
make your ListHandler use things like

    TYPE LISTS = POINTER TO NODES
         NODES = RECORD
                   Data : SYSTEM.ADDRESS;
                   Link : LISTS
                 END

    PROCEDURE Push (VAR List : LISTS; Item : SYSTEM.ADDRESS)
      VAR Latest : LISTS;
      BEGIN
        New(Latest);
        Latest^.Link := List; Latest^.Data := Item;
        List := Latest
      END Push;

.....

Then with

     VAR x : OneHandler.One;
         y : TwoHandler.Two
         OneStack, TwoStack : LISTS

BEGIN
     Push (OneStack, SYSTEM.ADDRESS(x)) (* type cast *);
     Push (TwoStack, SYSTEM.ADDRESS(y));

Depending on how your system interprets compatibility for opaques you
might even get away with the simpler code below:

(FST on the PC allows it, Logitech, JPI, FTL, Collier and Quickmod do not; all 
accept the code above.

     Push (OneStack, x);
     Push (TwoStack, y);
     
Of course, you don't gain type checking benefits any longer - and it is
even messier if you want to stack other than opaque types.

If you want to play with generics in M-2 the author to look for is John Gough,
both in his text book, and in articles in JPAM.
 




--  
uucp: uunet!m2xenix!puddle!5!494!4.101!Pat.Terry
Internet: Pat.Terry@p101.f4.n494.z5.fidonet.org
Technical Details
Message-ID: <64.26BC33BD@puddle.fidonet.org>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!umich!samsung!uakari.primate.wisc.edu!dali.cs.montana.edu!ogicse!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p101.f4.n494.z5.fidonet.org!Pat.Terry

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!umich!samsung!uakari.primate.wisc.edu!dali.cs.montana.edu!ogicse!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p101.f4.n494.z5.fidonet.org!Pat.Terry
From: Pat.Terry@p101.f4.n494.z5.fidonet.org (Pat Terry)
Newsgroups: comp.lang.modula2
Subject: Re: Implementing Abstract Lists
Message-ID: <64.26BC33BD@puddle.fidonet.org>
Date: 5 Aug 90 05:14:02 GMT
Sender: ufgate@puddle.fidonet.org (newsout1.26)
Organization: FidoNet node 5:494/4.101 - Settler City Fido, Grahamstown RSA
Lines: 73