[Oberon] [Fwd: RE: objects and jewels]

Jörg Straube joerg.straube at iaeth.ch
Wed Feb 20 08:57:26 CET 2013


Hi Srinivas

Sorry for the long mail...
As long as we look at STATIC data structures (data structure where the size is known at program start) I agree: pointers are strictly speaking not needed. And Oberon hides the pointers, e.g here

TYPE
  Point = RECORD x,y: REAL END;
  PointCollection = ARRAY 100 OF Point;

PROCEDURE Move(VAR p: Point); BEGIN INC(p.x, 2); INC(p.y, 3) END Move;

VAR
  canvas: PointCollection;
  i: INTEGER;
BEGIN Init(canvas); i:=0; REPEAT Move(canvas[i]); i:=i+1 UNTIL i=100 END

You don't see pointers although the paramater p of procedure Move is a pointer.

Now lets look at DYNAMIC data structures, Assume we don't know the size of it at program start, e.g a drawing program: you don't know how many points the artists wants to place on the canvas. One possibility would be to allocate a rather huge fix number of points (e.g 2000) and hope the artist will never reach your hard coded limit. Or you allocate memory for new points when it is needed.
How do we implement the PointCollection?

TYPE
  PointCollection = POINTER TO Point;
  Point = RECORD x,y: REAL; next: PointCollection END;

PROCEDURE Move(VAR p: Point); BEGIN INC(p.x, 2); INC(p.y, 3) END Move;

VAR
  canvas: PointCollection;
  i: PointCollection;
BEGIN Init(canvas); i:=canvas; REPEAT Move(i^); i:=i.next UNTIL i=NIL END

As you can see, I added the field "next" to implement the PointCollection as linked list. But more importantly, look at the REPEAT loop. In the static case above the variable i (INTEGER) was used as index into the PointCollection; "i" is not the Point itself but is the index into the PointCollection.

In the case of dynamic data structures you can see that also here "i" is the index into the PointCollection. Whether you call it POINTER or REFERENCE or INDEX to a certain type, is not so important, but it is something different than the base type, so in my point of view can not be hidden by the compiler.

Jörg

On 20.02.2013, at 06:36, Srinivas Nayak <sinu.nayak2001 at gmail.com> wrote:

> Now I understood...
> 
> But what is the need of it?
> Why we need to access one thing by two other things?
> 
> Looks like, at some point in programming,
> 1. we are trying the break the scoping rules and access it from outside.
> 2. we are trying to make an efficient way to pass by value.
> 
> Can't it be done using aliases?
> Is Oberon free from the nastiness of pointers as we found in C?
> 
> With thanks and best regards,
> 
> Yours sincerely,
> Srinivas Nayak
> 
> Home: http://www.mathmeth.com/sn/
> Blog: http://srinivas-nayak.blogspot.in/
> 
> 
> Douglas G. Danforth wrote:
>> Srinivas,
>> Here is some code as an example
>> 
>> MODULE FooModule;
>>    TYPE
>>        MyRecord =    RECORD n: INTEGER; r: REAL END;
>>    VAR
>>        a: POINTER TO MyRecord;
>>        b: POINTER TO MyRecord;
>> 
>>    BEGIN
>>        NEW(a);
>>        b := a;
>>        (*both a and b point to the same memory location*)
>> END FooModule.
>> 
>> On 2/19/2013 9:13 PM, Srinivas Nayak wrote:
>>> Dear Douglas,
>>> 
>>> I didn't get you.
>>> Will you please elaborate a bit?
>>> 
>>> With thanks and best regards,
>>> 
>>> Yours sincerely,
>>> Srinivas Nayak
>>> 
>>> Home: http://www.mathmeth.com/sn/
>>> Blog: http://srinivas-nayak.blogspot.in/
>>> 
>>> 
>>> Douglas G. Danforth wrote:
>>>> Srinivas,
>>>> 
>>>> With a pointer, two (or more) different variables can access the same
>>>> thing: a numeric value;
>>>> a record; a record with methods (Object).
>>>> 
>>>> -Doug Danforth
>>>> 
>>>> On 2/19/2013 8:17 PM, Srinivas Nayak wrote:
>>>>> One thing I couldn't understand, what was the need of pointers?
>>>> --
>>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
>>>> https://lists.inf.ethz.ch/mailman/listinfo/oberon
>>> --
>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
>>> https://lists.inf.ethz.ch/mailman/listinfo/oberon
>> 
>> --
>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
>> https://lists.inf.ethz.ch/mailman/listinfo/oberon
> 
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2334 bytes
Desc: not available
Url : https://lists.inf.ethz.ch/pipermail/oberon/attachments/20130220/bfa4dc17/attachment-0001.bin 


More information about the Oberon mailing list