Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!pa.dec.com!src.dec.com!src.dec.com!muller From: muller@src.dec.com (Eric Muller) Newsgroups: comp.lang.modula3 Subject: Re: Specializing objects Message-ID: <1991Feb14.231434.20859@src.dec.com> Date: 15 Feb 91 07:14:34 GMT References: <6929@rossignol.Princeton.EDU> Sender: news@src.dec.com (News) Reply-To: muller@src.dec.com (Eric Muller) Organization: DEC Systems Research Center Lines: 47 In article <6929@rossignol.Princeton.EDU>, nr@hart.Princeton.EDU (Norman Ramsey) asks: > > Suppose I have two object types > > TYPE > T = OBJECT > mumble, fritz: INTEGER; > END; > > TA = T OBJECT > a: BOOLEAN; > END; > > and suppose further that I am handed a T and want to produce a TA with > the property that every field of the TA has the same value it had in > the T. I can't think of anything any better than: > > PROCEDURE Narrow(o:T):TA = > BEGIN > RETURN NEW(TA, mumble := o.mumble, fritz := o.fritz, a := ); > END Narrow; > > Is there some way for me to do this without having to enumerate all > the fields of T? If not, I am bound to break my programs by extending > T and forgetting to update Narrow. Given that RTHeap has a Duplicate procedure: PROCEDURE Duplicate (r: REFANY): REFANY; (* return a reference to a new allocated copy of the referent, not recursively applied to internal references. Duplicate(r) is equivalent to Allocate (TYPECODE (r)). *) I could provide: PROCEDURE ?? (r1, r2: ROOT): ROOT; (* The type of r1 must be a subtype of the type of r2. The fields of r2 are (non-recursively) copied to the corresponding fields of r1 *) You could write: oa := ?? (NEW (TA, a = ), o); Opinions ? eric.