comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: tagged record child: override constructor?
Date: Tue, 13 Sep 2005 09:37:04 -0700
Date: 2005-09-13T09:37:04-07:00	[thread overview]
Message-ID: <ReDVe.2$Fv6.0@dfw-service2.ext.ray.com> (raw)
In-Reply-To: <1126591134.797303.318920@z14g2000cwz.googlegroups.com>

sean.gilbertson@gmail.com wrote:
> I have a tagged record that is declared private, along with a
> constructor function which returns an instance.  I do this to enforce
> the assignment of several required fields in the record, so if you know
> how to do this another way, please let me know!

It depends on what you mean by "enforce the assignment of several 
required fields". It is impossible to force the client to call your 
function, though you can arrange things so that the type is useless to 
the client if the client doesn't first call your function.

If you have

    type T is private;
    function Make return T;

then you don't need Make at all. You can ensure the required fields are 
initialized through default values:

private
    type T is record
       X : Integer   := -37;
       Y : Character := '*';
       Z : Duration;
    end record;

(Note that this is a property of record types in general; whether your 
record type is tagged doesn't affect this.) If you have fields you can't 
easily initialize through defaults, you can use the initialization 
features of controlled types:

private
    type T is new Ada.Finalization.Controlled with record
       ...
    end record;

    procedure Initialize (Object : in out T);

Initialize is called for every object of type T when it is created.

On the other hand, if you mean the assignment of client-supplied values 
to the required components, your choices are more limited. If the values 
are such that they may be discriminants, then that's a way to force the 
client to supply them:

    type T (X : Integer) is private;

If your required fields cannot be discriminants, then things get more 
complicated. You can include a Boolean field, default initialized to 
False, which is set to True by your function. All other operations of 
the type then check this field, and raise an exception if it is False:

    type T is private;

    procedure Initialize (X : out T; ...);

    Not_Initialized : exception;

    procedure Op (X : in out T);

private
    type T is record
       Initialized : Boolean := False;
       ...
    end record;

...

procedure Initialize (X : out T; ...) is
    ...
begin -- Initialize
    X.Initialized := True;
    ...
end Initialize;

procedure Op (X : in out T) is
    ...
begin -- Op
    if not X.Initialized then
       raise Not_Initialized;
    end if;

    ...
end Op;

This makes the type useless unless an object is first initialized.

None of this has anything to do with the fact that if T is tagged and a 
primitive operation of T is a function that returns T, then, yes, you do 
have to override such functions for each child of T.

-- 
Jeffrey Carter
"Now go away or I shall taunt you a second time."
Monty Python and the Holy Grail
E-mail: jeffrey_r_carter-nr [commercial-at]
         raytheon [period | full stop] com



  parent reply	other threads:[~2005-09-13 16:37 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-13  5:58 tagged record child: override constructor? sean.gilbertson
2005-09-13  6:39 ` David Trudgett
2005-09-13  7:32 ` Dmitry A. Kazakov
2005-09-13  7:56   ` tmoran
2005-09-13 15:23   ` sean.gilbertson
2005-09-13 17:37     ` Martin Krischik
2005-09-13 19:29       ` Ludovic Brenta
2005-09-14  7:49         ` Dmitry A. Kazakov
2005-09-14  9:05           ` Maciej Sobczak
2005-09-14 13:20             ` Dmitry A. Kazakov
2005-09-14 13:52               ` Hyman Rosen
2005-09-14 16:47                 ` Dmitry A. Kazakov
2005-09-14 17:16                   ` Hyman Rosen
2005-09-14 20:20                     ` Dmitry A. Kazakov
2005-09-14 20:34                       ` Georg Bauhaus
2005-09-14 20:56                       ` Hyman Rosen
2005-09-15  7:31                         ` Dmitry A. Kazakov
2005-09-15 13:19                           ` Hyman Rosen
2005-09-15 13:45                             ` Maciej Sobczak
2005-09-15 17:45                             ` Dmitry A. Kazakov
2005-09-15 18:54                               ` Hyman Rosen
2005-09-16  9:32                                 ` Dmitry A. Kazakov
2005-09-16 14:52                                   ` Hyman Rosen
2005-09-16 15:33                                     ` Jean-Pierre Rosen
2005-09-16 18:37                                       ` Hyman Rosen
2005-09-16 21:03                                     ` Dmitry A. Kazakov
2005-09-16 21:33                                       ` Hyman Rosen
     [not found]                                         ` <98ox2x9xvj9z.1uh92dslhvt4g.dlg@40tude.net>
2005-09-17 12:47                                           ` Georg Bauhaus
2005-09-17 15:56                                             ` Dmitry A. Kazakov
2005-09-14 16:14           ` Martin Krischik
2005-09-14 16:57             ` Dmitry A. Kazakov
2005-09-14 18:35               ` Martin Krischik
2005-09-14  9:28         ` Alex R. Mosteo
2005-09-14 16:10         ` Martin Krischik
2005-09-13  9:33 ` Georg Bauhaus
2005-09-13 16:37 ` Jeffrey Carter [this message]
2005-09-13 18:55   ` Robert A Duff
2005-09-13 22:18     ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox