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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Constructing an object Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Wed, 21 Sep 2005 13:55:34 +0200 Message-ID: <1lw8oz33ao529.12ep5ay7rg4oc$.dlg@40tude.net> NNTP-Posting-Date: 21 Sep 2005 13:55:34 MEST NNTP-Posting-Host: b6e6444e.newsread4.arcor-online.net X-Trace: DXC=:\LHLKlSaNUUEj^fn36YV^:ejgIfPPldTjW\KbG]kaMXFYk:AnJB[C]NK0oFROO;kY[6LHn;2LCV^7enW;^6ZC`T<=9bOTW=MN^ X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:4993 Date: 2005-09-21T13:55:34+02:00 List-Id: On Wed, 21 Sep 2005 10:46:35 +0200, Maciej Sobczak wrote: > I'm learning Ada and I would like to better understand the ways used to > create objects. > > 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; > > From what I've learnt ("Ada 95: The Craft of Object-Oriented > Programming" by John English), there are two possible ways to implement > "constructors" for these types: > > 1. By procedure, which is a primitive operation for each type in the > hierarchy, for example: > > procedure New_Shape(C : in Color; S : out Shape) is > begin > S.C := C; > end; > > procedure New_Rectangle(A, B : in Positive; C : in Color; R : out > Rectangle) is > begin > New_Shape(C, Shape(R)); > R.A := A; > R.B := B; > end; > > -- another version (btw - which version do you prefer?) > procedure New_Rectangle(A, B : in Positive; C : in Color; R : out > Rectangle) is > St : Shape; > begin > New_Shape(C, St); > R := (St with A => A, B => B); > end; > > > This does not please me much, because I can have uninitialized objects, > which may not make sense in the program: > > R : Rectangle; > > 2. By function: > > function New_Shape(C : in Color) return Shape is > S : Shape; > begin > S.C := C; > return S; > 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? > 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. > > What approach do you actually use in the real (non-tutorial) code? > Are there other techniques? 3. The default values: type Shape is tagged record C : Color := Black; -- This is initialized end record; 4. Initialization enforcement via discriminants: 4.a. Discriminants as members: type Shape (C : Color) is tagged null record; -- C has to be defined 4.b. Box-discriminants: type Shape (<>) is tagged private; -- Shape objects have to be initialized private type Shape is tagged record C : Color; end record; 5. Abstract types with private descendants and a factory: type Abstract_Shape is abstract tagged null record; function New_Shape (...) return Abstract_Shape'Class; -- Factory is the only public way to create it private type Shape is new Abstract_Shape with ...; > (I'm aware of the controlled types, but I need to provide parameters for > construction, which the special Initialize procedure does not have.) Alas, but Ada does not have user-defined constructors with parameters. There are only constructing functions. You can use 2+4.b. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de