From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Constructing an object
Date: Wed, 21 Sep 2005 13:55:34 +0200
Date: 2005-09-21T13:55:34+02:00 [thread overview]
Message-ID: <1lw8oz33ao529.12ep5ay7rg4oc$.dlg@40tude.net> (raw)
In-Reply-To: dgr6lb$f2k$1@sunnews.cern.ch
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
next prev parent reply other threads:[~2005-09-21 11:55 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-21 8:46 Constructing an object Maciej Sobczak
2005-09-21 10:16 ` Georg Bauhaus
2005-09-22 7:21 ` Maciej Sobczak
2005-09-21 11:55 ` Dmitry A. Kazakov [this message]
2005-09-22 7:28 ` Maciej Sobczak
2005-09-22 7:45 ` Maciej Sobczak
2005-09-22 13:33 ` Dmitry A. Kazakov
2005-09-24 5:23 ` Randy Brukardt
2005-09-24 9:47 ` Dmitry A. Kazakov
2005-09-29 0:12 ` Randy Brukardt
2005-09-29 8:17 ` Dmitry A. Kazakov
2005-09-29 22:21 ` Randy Brukardt
2005-09-30 8:14 ` Jean-Pierre Rosen
2005-09-30 19:28 ` Dmitry A. Kazakov
2005-09-30 17:49 ` Robert A Duff
2005-10-01 0:44 ` Randy Brukardt
2005-10-01 10:49 ` Dmitry A. Kazakov
2005-10-01 11:06 ` Tapio Kelloniemi
2005-10-01 14:13 ` Robert A Duff
2005-10-02 11:52 ` Tapio Kelloniemi
2005-10-01 15:19 ` Georg Bauhaus
2005-09-23 5:40 ` Matthew Heaney
2005-09-23 7:18 ` tmoran
2005-09-23 8:23 ` Maciej Sobczak
2005-09-23 12:04 ` Dmitry A. Kazakov
2005-09-23 12:36 ` Matthew Heaney
2005-09-23 13:03 ` Hyman Rosen
2005-09-23 13:41 ` Maciej Sobczak
2005-09-23 14:23 ` Matthew Heaney
2006-01-17 6:28 ` [Offtopic] " James Dennett
2005-09-23 13:42 ` Dmitry A. Kazakov
2005-09-23 14:27 ` Matthew Heaney
2005-09-23 12:24 ` Matthew Heaney
2005-09-24 5:34 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox