comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: abstract sub programs overriding
Date: Wed, 10 Mar 2004 21:43:08 -0500
Date: 2004-03-10T21:43:08-05:00	[thread overview]
Message-ID: <NqmdnWTI7OihT9Ld4p2dnA@comcast.com> (raw)
In-Reply-To: <1078949096.760629@master.nyc.kbcfp.com>

Hyman Rosen wrote:

> So he's not right, because it's not impossible, just very unusual.

No, he is right.  As a langauge lawyer I know how to "get around the 
rules."  But counting something that you can do in a child package of 
Ada.Controlled or Ada.Limited_Controlled as a property of the language 
is silly.  There are other ways to work around the rule, but they are 
similarly unlikely.  So things that can be done with a user defined type 
derived from some other user defined type are interesting, but in the 
end at least one Intialize procedure will be called for all user defined 
controlled objects.

> Anyway, let's see if I understand. You are saying that in Ada, it
> is unusual to call Parent.Initialize(Parent_Type(Child_Object)) from
> Child's Initialize. That seems odd to me; how does the child know that
> the parent can do without this call? If it can be so blithely ignored,
> what good is having it in the first place?

No, as I said, there are two common idioms, the first is to call the 
parent Initialize AFTER doing any initialization work needed on the 
fields that occur only in the child.  (The second is discussed below.)

> You also say that "It is much more common, however, for such a call to
> be done for only the parent part of the object, for example when creating
> a child object using an aggregate." Isn't it the case that when you do 
> that, Child's Initialize won't be called at all?

Yes, the parent object will be Initialized, and for a Controlled (not 
Limited_Controlled) aggregate assignment, the object created in this way 
will be Adjusted as a whole.  But I think you missed my main point, that 
the Initialize procedure for the child type may use an 
extension_aggregate to create the object, then make any necessary 
adjustments, either explicitly or implicitly through the Adjust call 
made for regular Controlled types.  Obviously you don't want the child's 
Initialize procedure to be called on extension_aggregates, because an 
extension_aggregate may necessary inside the Initialize procedure of the 
child type.

Sort of a meta comment here, and it goes back to what I said above.  All 
objects of types descended from Controlled or Limited_Controlled have 
fields that are not visible to an ordinary user.  This means that for 
most users what they can do in a child package of Ada.Controlled is of 
limited interest.  This is really what makes the only interesting cases:

procedure Initalize(Object: in out Child) is
begin
   Do_Something(Object);
   Initialize(Parent(Object));
end Initialize;

or:

procedure Initialize(Object: in out Child) is
begin
   Object := (Parent_Type with ...);
   Do_Something_Else(Object);
end Initialize;

Now notice that it is trivially obvious that in Do_Something or 
Do_Something_Else, Object is not fully initialized.  But we are inside 
the Initialize procedure, so of course it hasn't completed yet.  As a 
programmer it is not hard to choose whichever idiom is appropriate for 
the type you are defining.  But it is also the case that for any 
controlled Object users cannot bypass the first stage of initialization, 
where Object is created and all components are initialized to their 
default values. (Or in the extension aggregate case, given valid 
non-default values.)

So yes, there are areas where user written code can access an object 
that has not yet been completely initialized.  But in practice, as a 
user you will find that once you get rid of any bugs (and exceptions) 
inside Intialize, Adjust, and Finalize, that code written by others, 
including the authors of types derived from your types, cannot mess up 
the invariants of your type.

> You also say that as a matter of policy, Initialize should not make
> a dispatching calls because it is likely to be incorrect. That's what
> C++ avoids by preventing dispatching from going to parts of the final
> object which are not yet constructed.

No, I did not say that.  I said that attempts to down convert an object 
to a child type will fail. Technically, it is not possible to do a VALUE 
conversion from a parent type to a (tagged) child type.  You will find 
that in some cases a view conversion is legal, but an attempt to assign 
to that view will often raise Constraint_Error (or Program_Error).

I am not going to try and describe here when conversions are illegal, 
when they will raise Program_Error, and when they will raise 
Constraint_Error.  All that is spelled out in RM 4.6, and it is hard to 
say it in fewer words. (RM 4.6 is cryptic enough!  You might want to use 
the AARM instead: http://www.adaic.org/standards/95aarm/html/AA-4-6.html)

So what I was saying was that it is possible to put an view conversion 
to a child type inside an Initialize procedure, but if you try to write 
the code, you will find yourself dodging lots of land mines.  For 
example, the body of the package which defines the parent type is going 
to have to with the package that defines the child type.  But if you are 
not very careful, there will be no valid elaboration order for the 
corresponding package bodies.  There are lots of land mines that have 
nothing to do with the issues you are trying to raise here that can come 
up if you try what you indicated.

It is like discussing the speed at which you can drive southbound down 
the northbound side of a divided highway.  You may be interested in 
whether or not the banking of the curves works correctly.  However, 
there are going to be lots of impediments to doing the actual experiment 
that have nothing to do with curves, banking, or automobile suspensions. 
  (They would have something to do with license suspensions though. ;-)

-- 
                                           Robert I. Eachus

"The only thing necessary for the triumph of evil is for good men to do 
nothing." --Edmund Burke




  reply	other threads:[~2004-03-11  2:43 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-02 19:01 abstract sub programs overriding Evangelista Sami
2004-03-03  1:43 ` Stephen Leake
2004-03-05 15:02   ` Evangelista Sami
2004-03-05 16:15     ` Marius Amado Alves
2004-03-08 18:54       ` Adam Beneschan
2004-03-08 23:42         ` Marius Amado Alves
2004-03-05 16:26     ` Marius Amado Alves
2004-03-06  9:31       ` Simon Wright
2004-03-06 15:18         ` Evangelista Sami
2004-03-06 19:09           ` Marius Amado Alves
2004-03-07 12:35             ` Simon Wright
2004-03-07 13:39               ` Marius Amado Alves
2004-03-08 19:08               ` Adam Beneschan
2004-03-08 20:03                 ` Hyman Rosen
2004-03-09  8:51                   ` Dmitry A. Kazakov
2004-03-09 13:34                     ` Hyman Rosen
2004-03-09 14:49                       ` Dmitry A. Kazakov
2004-03-09 15:14                         ` Hyman Rosen
2004-03-09 15:56                           ` Dmitry A. Kazakov
2004-03-09 16:32                             ` Hyman Rosen
2004-03-10  9:32                               ` Dmitry A. Kazakov
2004-03-10 13:08                                 ` Hyman Rosen
2004-03-10 14:58                                   ` Robert I. Eachus
2004-03-10 16:00                                     ` Hyman Rosen
2004-03-10 18:07                                       ` Robert I. Eachus
2004-03-10 20:04                                         ` Hyman Rosen
2004-03-11  2:43                                           ` Robert I. Eachus [this message]
2004-03-11 13:55                                             ` Hyman Rosen
2004-03-12 23:02                                               ` Robert I. Eachus
2004-03-14 21:33                                                 ` Hyman Rosen
2004-03-15  5:59                                                   ` Robert I. Eachus
2004-03-15 14:39                                                     ` Hyman Rosen
2004-03-16 16:16                                                       ` Robert I. Eachus
2004-03-16 16:51                                                         ` Hyman Rosen
2004-03-16 19:54                                                         ` Hyman Rosen
2004-03-16 23:16                                                           ` Randy Brukardt
2004-03-17  1:54                                                           ` Robert I. Eachus
2004-03-16 23:14                                                         ` Randy Brukardt
2004-03-17  2:43                                                           ` Robert I. Eachus
2004-03-17 17:40                                                             ` Randy Brukardt
2004-03-18  2:39                                                               ` Robert I. Eachus
2004-03-18  5:57                                                                 ` Randy Brukardt
2004-03-18 15:03                                                                   ` Hyman Rosen
2004-03-18 20:32                                                                     ` Randy Brukardt
2004-03-19  3:59                                                                       ` Hyman Rosen
2004-03-19 19:37                                                                         ` Randy Brukardt
2004-03-16  6:00                                               ` Randy Brukardt
2004-03-11 10:09                                   ` Dmitry A. Kazakov
2004-03-11 14:10                                     ` Hyman Rosen
2004-03-11 14:59                                       ` Dmitry A. Kazakov
2004-03-11 15:40                                         ` Hyman Rosen
2004-03-11 16:28                                           ` Dmitry A. Kazakov
2004-03-11 17:26                                             ` Hyman Rosen
2004-03-12  8:53                                               ` Dmitry A. Kazakov
2004-03-12 13:09                                                 ` Hyman Rosen
2004-03-12 14:00                                                   ` Dmitry A. Kazakov
2004-03-12 14:56                                                     ` Hyman Rosen
2004-03-12 18:19                                                       ` Dmitry A. Kazakov
2004-03-12 18:34                                                         ` Hyman Rosen
2004-03-12 20:05                                                           ` Georg Bauhaus
2004-03-13 10:12                                                           ` Dmitry A. Kazakov
2004-03-12 18:07                                               ` Robert I. Eachus
2004-03-10 15:51                 ` Evangelista Sami
2004-03-11  1:38                   ` Dan Eilers
2004-03-06 23:20     ` Dan Eilers
2004-03-03 12:00 ` Marius Amado Alves
2004-03-13  7:51 ` Simon Wright
replies disabled

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