From: fn00@gte.com (Farshad Nayeri)
Newsgroups: comp.lang.modula3
Subject: Re: references, objects & m3
Date: 25 Jan 93 13:56:01 GMT
	<MOSS.93Jan24164146@CRAFTY.cs.cmu.edu>
	<C1E30F.vx@undergrad.math.waterloo.edu>
Organization: GTE Laboratories, Waltham, Massachusetts, USA
In-reply-to: papresco@undergrad.math.waterloo.edu's message of 25 Jan 93 03:00:07 GMT

In article <C1E30F.vx@undergrad.math.waterloo.edu> papresco@undergrad.math.waterloo.edu (Paul Prescod) writes:

   In article <MOSS.93Jan24164146@CRAFTY.cs.cmu.edu> moss@cs.cmu.edu writes:

   >I don't understand why you're using REF OBJECT ... END instead of
   >just OBJECT ... END. The latter is implementation essentially as
   >REF RECORD ... END, but with the additional semantics of OBJECT
   >method invocation, subtyping, etc. You should probably just delete
   >the level of REF in your binary tree stuff. Maybe you want
   >somehting like this:

   Thanks for your help.  Someone else pointed out to me that objects ARE
   references.

   I'm not sure if I like that.  For instance, how do I copy an object?
   The copy will point to the same data fields, right?  

I wonder why do you need to copy objects. I know that copying objects
is pretty usual in C++ programming: since there is no garbage
collection, programmers copy object _contents_ (and not object
_references_) to reduce sharing within the "object graph". This way
the source pointer can be disposed without worrying about who else
references the object. Modula-3 has grabage collection, so you seldom
have to copy objects.

   Also is there anyway to auto-initialize/auto-destroy objects as in C++
   with constructors and destructors?

Not really (except for static initializations).  This is a matter of
religeous war among Modula-3 purists and people with C++ orientation.
I hope that I won't be starting a war-thread on this subject, when I
say that:

- Because of garbage collection, destructors are not as crucial in
  Modula-3 programming. (What do you usually do in a destructor in
  C++?)

- Constructor-like procedures can be created by doing:

  MODULE Foo;

  TYPE T = OBJECT val: INTEGER := -1 END;

  PROCEDURE New (val: INTEGER): T =
  BEGIN
    RETURN NEW (T, val := val);
  END New;
  
  BEGIN
  END Foo.

The "New" procedure can be exported to other modules.

Another way to achieve the same thing is to have:

  MODULE Foo;

  TYPE T = OBJECT val: INTEGER; METHODS init (val:INTEGER): T := Init; END;

  PROCEDURE Init (self: T; val: INTEGER): T =
  BEGIN
    self.val := val;
    RETURN self;
  END Init;

  BEGIN
  END Foo.


Now you can do something like:
   
  VAR t := NEW (Foo.T).init(20);

or if you want to subclass T, you can do

  TYPE 
    S = Foo.T OBJECT 
      name: TEXT; 
    OVERRIDES 
      init (val: INTEGER; name: TEXT) := Init;
    END;

  PROCEDURE Init (self: S; val: INTEGER; name: TEXT) = 
  BEGIN
    Foo.T.init (self);   (* call init of the superclass *)
    self.name := name;
  END Init;

    
Yes, this is not exactly a constructor, and no, there are no public
plans to add constructors and destructors (or anything else for that
matter ;-) to Modula-3.

Hope this helps.

--farshad

Disclaimer: enclosed code has not been compiled.

--
Farshad Nayeri                Intelligent Database Systems
fn00@gte.com                  Computer and Intelligent Systems Laboratory
(617)466-2473                 GTE Laboratories, Waltham, MA

  "To see is to forget the name of the thing one sees." -- Paul Valery