Re: Receiver restrictions

Stephan Gehring (gehring@interval.com)
Wed, 23 Dec 1998 17:52:08 -0800

At 04:46 PM 12/23/98, you wrote:
>PROCEDURE (VAR ls: List) ClearList*, NEW;
>BEGIN
> ls := NIL
>END ClearList;

Hmm. This is scary. A list committing suicide? :-)

>Each of these requires that the pointer in the receiver (possibly)
>be changed, but the language does not permit VAR with the pointer
>in the receiver. (1) Why does CP have the restriction, and (2) how
>can I do this without resorting to proper procedures, where VAR with
>a pointer is allowed? My problem is to introduce linked lists to
>beginning programming students using methods, and I would rather not
>mix procedures and methods (and definitely not polymorphism at this
>point).

(1) I believe this is a restriction of Oberon-2 and not particularly of CP.
I don't know the exact reasoning behind this, but you may well have
demonstrated just why this is a bad idea.

(2) If you insist on an object model for lists consider the following. A
list *is not* a sequence of list elements, but *has* a sequence of
elements. As such, it is its own entity. If you follow this model, the
declarations look something like below

TYPE
T* = ARRAY 16 OF CHAR;
ListElement = POINTER TO RECORD
value: T;
link: ListElement
END;
List* = RECORD
first: ListElement
END;

PROCEDURE (VAR ls: List) ClearList*, NEW;
BEGIN
ls.first := NIL
END ClearList;

....

Hope this helps.

Merry Christmas to all, Stephan

--------------------------------------------------------------
Stephan Gehring ++1 (650) 842 6233