Re: [BLACKBOX] Polymorphism

From: [at]} <Alexander>
Date: Fri, 21 Nov 2008 10:29:11 +0600

----boundary-LibPST-iamunique-827000504_-_-
Content-type: text/plain

Hello!

AMAUN> In writing my List module, I have a method DoEach that walks the list,
AMAUN> and applies method Do to each element.
AMAUN> (this: List) Do (VAR params: ANYREC), NEW, EMPTY;
AMAUN> (root: List) DoEach (VAR params: ANYREC), NEW;

AMAUN> In moving some existing software to the new library module, it turns
AMAUN> out that I wish to apply a few different procedures to the list. In
AMAUN> trying to avoid procedure variables, I find I am drawing a blank.
AMAUN> Making several lateral extensions to the base record may be an
AMAUN> approach, particularly if there is a pointer to a record with that
AMAUN> particular implementation as one of the fields in params.

  I would suggest either using a procedure variable parameter for the
  DoEach call (less flexible) or the following approach. The idea is
  to separate Do from the List in a new RECORD:

  Doer* = EXTENSIBLE RECORD END;
  PROCEDURE (VAR this: Doer) Do- (VAR elem: ListElem), NEW, EMPTY;

  PROCEDURE (root: List) DoEach* (VAR doer: Doer), NEW;
  (* for each element of the list call doer.Do *)

  Usage:
  In a using module declare a Doer extension and implement Do. E.g.:

  DateSetter = RECORD (Lists.Doer)
    date: Date; (* any additional parameters for the Do call are
    declared as fields for the Doer extension *)
  END;

  PROCEDURE (this: DateSetter) Do (VAR elem: Lists.ListElem);
  BEGIN
     elem.date := this.date
  END Do;

  (* Usage example *)

  PROCEDURE SetDate (list: Lists.List; VAR date: Date);
  VAR ds: DateSetter;
  BEGIN
     ds.date := date;
     list.DoEach (ds)
  END SetDate;

---=====---
 Alexander


----
To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy----boundary-LibPST-iamunique-827000504_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"
e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4g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Rpb25hbCBwYXJhbWV0ZXJzIGZvciB0aGUgRG8gY2FsbCBhcmVccGFyDQogICAgZGVj
bGFyZWQgYXMgZmllbGRzIGZvciB0aGUgRG9lciBleHRlbnNpb24gKilccGFyDQogIEVORDtccGFy
DQpccGFyDQogIFBST0NFRFVSRSAodGhpczogRGF0ZVNldHRlcikgRG8gKFZBUiBlbGVtOiBMaXN0
cy5MaXN0RWxlbSk7XHBhcg0KICBCRUdJTlxwYXINCiAgICAgZWxlbS5kYXRlIDo9IHRoaXMuZGF0
ZVxwYXINCiAgRU5EIERvO1xwYXINClxwYXINCiAgKCogVXNhZ2UgZXhhbXBsZSAqKVxwYXINClxw
YXINCiAgUFJPQ0VEVVJFIFNldERhdGUgKGxpc3Q6IExpc3RzLkxpc3Q7IFZBUiBkYXRlOiBEYXRl
KTtccGFyDQogIFZBUiBkczogRGF0ZVNldHRlcjtccGFyDQogIEJFR0lOXHBhcg0KICAgICBkcy5k
YXRlIDo9IGRhdGU7XHBhcg0KICAgICBsaXN0LkRvRWFjaCAoZHMpXHBhcg0KICBFTkQgU2V0RGF0
ZTtccGFyDQpccGFyDQotLS09PT09PS0tLVxwYXINCiBBbGV4YW5kZXJccGFyDQpccGFyDQpccGFy
DQotLS0tXHBhcg0KVG8gdW5zdWJzY3JpYmUsIHNlbmQgYSBtZXNzYWdlIHdpdGggYm9keSAiU0lH
Tk9GRiBCTEFDS0JPWCIgdG8gTElTVFNFUlZATElTVFMuT0JFUk9OLkNIfX0AU1RTLg==
----boundary-LibPST-iamunique-827000504_-_---
Received on Fri Nov 21 2008 - 05:29:11 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:30:51 UTC