comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: tagged record child: override constructor?
Date: Wed, 14 Sep 2005 15:20:44 +0200
Date: 2005-09-14T15:20:40+02:00	[thread overview]
Message-ID: <13wyu4lwsmzmz.ktc3t2av54yv$.dlg@40tude.net> (raw)
In-Reply-To: dg8p45$74f$1@sunnews.cern.ch

On Wed, 14 Sep 2005 11:05:10 +0200, Maciej Sobczak wrote:

> Dmitry A. Kazakov wrote:
> 
>> There is no difference between T and
>> T'Class (the source of countless problems.)
> 
> Could you elaborate?

1. It is space inefficient. To have T and T'Class same requires keeping the
type tag in all Ts, while it is only necessary in T'Class.

2. There is no chance to get a consistent type system where each specific
type would have classes and primitive operations. If Boolean and
Boolean'Class have to be same, then the size of Boolean cannot be 1 bit.
(Presently Ada does not use this opportunity, but I hope it some day will)

3. It is inefficient, because it has to dispatch everywhere (except for
constructors and destructors, which still have the overhead of unused
dispatching.) Maybe this is the reason why so many people believe that OO
isn't suitable for real-time. I believe that this is rather an ungrounded
projection from C++ to Ada.

4. It is error-prone because of enforced re-dispatch (see below.) One
should explicitly specify T::Foo() referring member functions within a
member function. In practice nobody cares to do this, which often leads to
nasty surprises.

5. When T and T'Class are same, objects have identity. This identity might
be arbitrary. Nevertheless people will exploit it. This leads to bad
fragile design. Another form of identity is the object's address. Which is
quite similar case. Here again Ada prevents you from occasional using this
sort of identity (you have to make the object aliased), while C++
encourages it (you can apply & to almost everything.)

>> So all operations re-dispatch
> 
> What does that mean?

type Base is tagged ...;
procedure Foo (X : Base); -- Primitive operation
procedure Bar (X : Base);  -- Another primitive operation

procedure Foo (X : Base) is
begin
   Bar (X); -- This does not dispatch! It calls Base.Bar
end Foo;

The implementation of Foo stay consistent no matter what a derived type
would do with Bar. Note that this is nothing but a trivial contract model
which C++ lacks in this case. Within Foo, X is declared of the type Base.
So it would be infeasible to dispatch again. The type is known. If dispatch
is really needed, then that should be specified in the contract. I.e. the
type of X should be Base'Class. [Ada allows explicit conversions to
Base'Class, a bad idea though. Better pattern is to make the objects
explicitly having identity, using the Rosen's trick.]

Re-dispatch usually indicates a design problem.

>> except for constructors and destructors, which don't.
> 
> They do, but there the dynamic and static type of the object is the 
> same, which actually saves a lot of problems.

Yes, they do as if they wouldn't. To me it is that they don't.

Of course this saves the problems just because there is no re-dispatch,
which is in general a bad thing. Actually what C++ does in constructors and
destructors is using Ada's (consistent) model.

>> I.e. it is rather C++
>> where the call site determines whether a virtual function dispatches.
> 
> It always does, independent on the call site. It is rather the dynamic 
> type of the object that changes during construction and destruction, 
> thus leading to the "virtual functions don't work" impression.

Yes. It changes from T'Class to T and backwards depending on the location,
utterly inefficient and very error-prone.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2005-09-14 13:20 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 [this message]
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
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