Re: [BLACKBOX] Variant Records vs. Component Pascal

From: [at]} <Robert>
Date: Sun, 3 Jan 2010 23:11:03 +0000

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

On 31/12/2009 21:12, Stephen R. Troy wrote:

> But I apparently I can't use this method in Component Pascal because
> variant records are not allowed. I tried defining a basic record as an
> ABSTRACT type, then defining each of my three record types as
> extensions. However, I can't instantiate a record as a NEW(pointer to
> basic type), and I can't use a single sort routine with each of the
> three (pointer to extension type) -- I need three separate routines,
> which is clumsy at best, and seems contrary to the basic intent of
> Component Pascal.

The first thing to say is that what you are trying to do will be easy, but
only after making the Modula-2 to Oberon changes in thinking and notation.

You sound as if you are on the right track, but not quite there yet.

I shall image your three "variants" are called A, B, & C; each has a field (of
maybe different types) called aVal, bVal, & cVal, and you want to sort using
these fields.

Some code ...

BaseType = POINTER TO ABSTRACT RECORD
               next, prev : BaseType;
              END;

A = POINTER TO RECORD (BaseType) aVal : Atype END;
B = POINTER TO RECORD (BaseType) bVal : Btype END;
C = POINTER TO RECORD (BaseType) cVal : Ctype END;

Now you can't (and don't want to) do NEW (baseType), but you can do NEW
on values of types A, B, & C.

First problem solved - I hope?


Now for the interesting bit, an extension aware sorting routine.

Maybe your sort routine wants to look like:

PROCEDURE Sort (base : BaseType)
   VAR
     p, q : BaseType
   BEGIN
     ...
     IF p.val < q.val THEN ... END;
     ...
   END Sort;

But p & q don't have a '.val' field!!!

So replace that line by

     IF p.PriorTo (q) THEN ... END;

and write the four routines

PROCEDURE (p: BaseType) PriorTo (q: BaseType) : BOOLEAN, NEW, ABSTRACT;

PROCEDURE (p: A) PriorTo (q: BaseType) : BOOLEAN;
   BEGIN
     RETURN p.aVal < q(A).aVal
   END PriorTo;

PROCEDURE (p: B) PriorTo (q: BaseType) : BOOLEAN;
   BEGIN
     RETURN p.bVal < q(B).bVal
   END PriorTo;

PROCEDURE (p: C) PriorTo (q: BaseType) : BOOLEAN;
   BEGIN
     RETURN p.cVal < q(C).cVal
   END PriorTo;


You will find a fully worked out library module, with documentation, on the CPC
site.
Download the 'Lib' subsystem and look at the module Cycles.


In summary, you only need 1 Sort routine, but you do need 3 PriorTo
routines if they work on different fields that do not belong to the base type.


Regards

Robert


----
To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy----boundary-LibPST-iamunique-1249260476_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"
e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwIE9uIDMxLzEyLzIwMDkgMjE6MTIsIFN0ZXBoZW4g
Ui4gVHJveSB3cm90ZTpccGFyDQpccGFyDQo+IEJ1dCBJIGFwcGFyZW50bHkgSSBjYW4ndCB1c2Ug
dGhpcyBtZXRob2QgaW4gQ29tcG9uZW50IFBhc2NhbCBiZWNhdXNlXHBhcg0KPiB2YXJpYW50IHJl
Y29yZHMgYXJlIG5vdCBhbGxvd2VkLiBJIHRyaWVkIGRlZmluaW5nIGEgYmFzaWMgcmVjb3JkIGFz
IGFuXHBhcg0KPiBBQlNUUkFDVCB0eXBlLCB0aGVuIGRlZmluaW5nIGVhY2ggb2YgbXkgdGhyZWUg
cmVjb3JkIHR5cGVzIGFzXHBhcg0KPiBleHRlbnNpb25zLiBIb3dldmVyLCBJIGNhbid0IGluc3Rh
bnRpYXRlIGEgcmVjb3JkIGFzIGEgTkVX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Ugb25seSBuZWVkIDEgU29ydCByb3V0aW5lLCBidXQgeW91IGRvIG5l
ZWQgMyBQcmlvclRvXHBhcg0Kcm91dGluZXMgaWYgdGhleSB3b3JrIG9uIGRpZmZlcmVudCBmaWVs
ZHMgdGhhdCBkbyBub3QgYmVsb25nIHRvIHRoZSBiYXNlIHR5cGUuXHBhcg0KXHBhcg0KXHBhcg0K
UmVnYXJkc1xwYXINClxwYXINClJvYmVydFxwYXINClxwYXINClxwYXINCi0tLS1ccGFyDQpUbyB1
bnN1YnNjcmliZSwgc2VuZCBhIG1lc3NhZ2Ugd2l0aCBib2R5ICJTSUdOT0ZGIEJMQUNLQk9YIiB0
byBMSVNUU0VSVkBMSVNUUy5PQkVST04uQ0h9fQBBQ0tC
----boundary-LibPST-iamunique-1249260476_-_---
Received on Mon Jan 04 2010 - 00:11:03 UTC

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