home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / modula3 / 1146 < prev    next >
Encoding:
Internet Message Format  |  1993-01-25  |  3.6 KB

  1. Path: sparky!uunet!noc.near.net!ceylon!newshost!fn00
  2. From: fn00@gte.com (Farshad Nayeri)
  3. Newsgroups: comp.lang.modula3
  4. Subject: Re: references, objects & m3
  5. Message-ID: <FN00.93Jan25085601@tahoe.gte.com>
  6. Date: 25 Jan 93 13:56:01 GMT
  7. References: <C1BsMI.5Ay@undergrad.math.waterloo.edu>
  8.     <MOSS.93Jan24164146@CRAFTY.cs.cmu.edu>
  9.     <C1E30F.vx@undergrad.math.waterloo.edu>
  10. Sender: news@ceylon.gte.com
  11. Organization: GTE Laboratories, Waltham, Massachusetts, USA
  12. Lines: 105
  13. In-reply-to: papresco@undergrad.math.waterloo.edu's message of 25 Jan 93 03:00:07 GMT
  14.  
  15. In article <C1E30F.vx@undergrad.math.waterloo.edu> papresco@undergrad.math.waterloo.edu (Paul Prescod) writes:
  16.  
  17.    In article <MOSS.93Jan24164146@CRAFTY.cs.cmu.edu> moss@cs.cmu.edu writes:
  18.  
  19.    >I don't understand why you're using REF OBJECT ... END instead of
  20.    >just OBJECT ... END. The latter is implementation essentially as
  21.    >REF RECORD ... END, but with the additional semantics of OBJECT
  22.    >method invocation, subtyping, etc. You should probably just delete
  23.    >the level of REF in your binary tree stuff. Maybe you want
  24.    >somehting like this:
  25.  
  26.    Thanks for your help.  Someone else pointed out to me that objects ARE
  27.    references.
  28.  
  29.    I'm not sure if I like that.  For instance, how do I copy an object?
  30.    The copy will point to the same data fields, right?  
  31.  
  32. I wonder why do you need to copy objects. I know that copying objects
  33. is pretty usual in C++ programming: since there is no garbage
  34. collection, programmers copy object _contents_ (and not object
  35. _references_) to reduce sharing within the "object graph". This way
  36. the source pointer can be disposed without worrying about who else
  37. references the object. Modula-3 has grabage collection, so you seldom
  38. have to copy objects.
  39.  
  40.    Also is there anyway to auto-initialize/auto-destroy objects as in C++
  41.    with constructors and destructors?
  42.  
  43. Not really (except for static initializations).  This is a matter of
  44. religeous war among Modula-3 purists and people with C++ orientation.
  45. I hope that I won't be starting a war-thread on this subject, when I
  46. say that:
  47.  
  48. - Because of garbage collection, destructors are not as crucial in
  49.   Modula-3 programming. (What do you usually do in a destructor in
  50.   C++?)
  51.  
  52. - Constructor-like procedures can be created by doing:
  53.  
  54.   MODULE Foo;
  55.  
  56.   TYPE T = OBJECT val: INTEGER := -1 END;
  57.  
  58.   PROCEDURE New (val: INTEGER): T =
  59.   BEGIN
  60.     RETURN NEW (T, val := val);
  61.   END New;
  62.   
  63.   BEGIN
  64.   END Foo.
  65.  
  66. The "New" procedure can be exported to other modules.
  67.  
  68. Another way to achieve the same thing is to have:
  69.  
  70.   MODULE Foo;
  71.  
  72.   TYPE T = OBJECT val: INTEGER; METHODS init (val:INTEGER): T := Init; END;
  73.  
  74.   PROCEDURE Init (self: T; val: INTEGER): T =
  75.   BEGIN
  76.     self.val := val;
  77.     RETURN self;
  78.   END Init;
  79.  
  80.   BEGIN
  81.   END Foo.
  82.  
  83.  
  84. Now you can do something like:
  85.    
  86.   VAR t := NEW (Foo.T).init(20);
  87.  
  88. or if you want to subclass T, you can do
  89.  
  90.   TYPE 
  91.     S = Foo.T OBJECT 
  92.       name: TEXT; 
  93.     OVERRIDES 
  94.       init (val: INTEGER; name: TEXT) := Init;
  95.     END;
  96.  
  97.   PROCEDURE Init (self: S; val: INTEGER; name: TEXT) = 
  98.   BEGIN
  99.     Foo.T.init (self);   (* call init of the superclass *)
  100.     self.name := name;
  101.   END Init;
  102.  
  103.     
  104. Yes, this is not exactly a constructor, and no, there are no public
  105. plans to add constructors and destructors (or anything else for that
  106. matter ;-) to Modula-3.
  107.  
  108. Hope this helps.
  109.  
  110. --farshad
  111.  
  112. Disclaimer: enclosed code has not been compiled.
  113.  
  114. --
  115. Farshad Nayeri                Intelligent Database Systems
  116. fn00@gte.com                  Computer and Intelligent Systems Laboratory
  117. (617)466-2473                 GTE Laboratories, Waltham, MA
  118.  
  119.   "To see is to forget the name of the thing one sees." -- Paul Valery
  120.