comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Constructing an object
Date: Fri, 23 Sep 2005 12:24:08 GMT
Date: 2005-09-23T12:24:08+00:00	[thread overview]
Message-ID: <ud5n0t1jv.fsf@earthlink.net> (raw)
In-Reply-To: dh0e16$ghc$1@sunnews.cern.ch

Maciej Sobczak <no.spam@no.spam.com> writes:

> Is it really enough? Consider this:
>
> with Shapes;
> procedure Hello is
>     S : Shapes.Shape;  -- oops, uninitialized Shape
> begin
>     null;
> end Hello;

I should have been more specific.  In addition to declaring the type as
private, you have to either provide a default for the C component of the
Shape record, or privately derive from Controlled and assign C a value
in the Initialize operation.


> It compiles fine (GNAT) and it allows me to declare an uninitialized
> object of private type Shape. The only thing that private type gives me
> is that I cannot tinker with its internals directly. This is good in
> itself, but not enough to save me from having uninitialized objects.

Right.


> I think that the most general approach is with abstract types, 

You don't need abstract types for this problem.  You would only need to
declare the type abstract if you were declaring an interface (indeed, in
Ada 2005 there's even a special keyword for that), for which you're
unable to provide an implemention for some of the operations.


> although
> at the same time it looks like an overkill in simpler cases and not
> without its own problems. In particular, it introduces an artifical
> hierarchy of types (not counting the cases where the hierarchy already
> exists at the design time, like with true Shapes, Animals, etc.).

Right.  In this case, you don't need an abstract type to be the root of
your type hierarchy.


> I miss constructors. The "real" ones. ;-) (no flame intended)

Well, this issue has been discussed for a long time.  In Ada you use a
function for this purpose.  The only snag is if this is a tagged type,
then you have to treat value-returning functions with care.  You
probably want to make the function non-primitive.  (Note that this is an
error in my last post.)  That means either wrap the value-returning
functions in a nested package (or possibly as children), or return a
class-wide type.

The big improvement in Ada 2005 is that there's new syntax for
initializing a return value (I showed it in one of my examles), that you
can use even for limited types.  (Only for limited types?  Hopefully an
ARG member will jump in here and, er, elaborate.)



  parent reply	other threads:[~2005-09-23 12:24 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
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 [this message]
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