From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2e2db8edf2656165 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!newsread3.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: Matthew Heaney@MHEANEYIBMT43 Newsgroups: comp.lang.ada Subject: Re: Constructing an object References: From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 23 Sep 2005 05:40:43 GMT NNTP-Posting-Host: 24.149.57.125 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.atl.earthlink.net 1127454043 24.149.57.125 (Thu, 22 Sep 2005 22:40:43 PDT) NNTP-Posting-Date: Thu, 22 Sep 2005 22:40:43 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news1.google.com comp.lang.ada:5052 Date: 2005-09-23T05:40:43+00:00 List-Id: Maciej Sobczak writes: > Let's say I have the following: > > type Color is (Black, Red, Green, Blue, White); > > type Shape is tagged record > C : Color; > end record; > > type Rectangle is new Shape with record > A, B : Positive; > end record; If you want greater control of object creation and initialization, then you could make the types private: package Shapes is type Color is ...; type Shape is tagged private; type Rectangle is new Shape with private; function New_Shape (C : Color) return Shape; function New_Rectangle (C : Color) return Rectangle; -- other ctors with dif't params as desired private type Shape is tagged record ...; type Rectangle is new Shape with record ...; end Shapes; > 1. By procedure, which is a primitive operation for each type in the > hierarchy, for example: Use a function. In Ada 2005, you can use a function even for limited types. > 2. By function: > > function New_Shape(C : in Color) return Shape is > S : Shape; > begin > S.C := C; > return S; > end; Yes, use a function. However (and I'm really thinking of controlled types here) you might want to use aggregate syntax: function New_Shape (C : Color) return Shape is begin return Shape'(C => C); end; In Ada 2005, you'll be able say (I don't know if my syntax is quite right): function New_Shape (C : Color) return Shape is begin return S : Shape do S.C := C; end return; end; > function New_Rectangle(A, B : in Positive; C : in Color) return > Rectangle is > R : Rectangle; > begin > Shape(R) := New_Shape(C); -- is this OK? I think this is legal; it's called a "view conversion." > R.A := A; > R.B := B; > return R; > end; > > This does not please me for the same reason - no enforcement of proper > initialization, if a given type makes no sense uninitialized. But that's because you didn't declare the type in such a way that initialization is enforced. You have a few possibilities, including declaring the type as private, declaring it as private and with an unknown discriminant, declaring it with a known discriminant, or giving the record components default values. > What approach do you actually use in the real (non-tutorial) code? > Are there other techniques? > (I'm aware of the controlled types, but I need to provide parameters for > construction, which the special Initialize procedure does not have.) Not quite. You can combine a function as above, but make the type controlled. Something like: package Shapes is type Shape is tagged private; function New_Shape (C : Color) return Shape; private type Shape is new Controlled with record C : Color; end record; procedure Initialize (S : in out Shape); end; package body Shapes is function New_Shape (C : Color) return Shape is begin return Shape'(Controlled with C => C); end;