comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Dynamich Dispatching...
Date: Sat, 16 Oct 2004 21:25:53 +0200
Date: 2004-10-16T21:25:53+02:00	[thread overview]
Message-ID: <6ne5n2fgwz1x.a4ecpxmxmlr9.dlg@40tude.net> (raw)
In-Reply-To: 417013a9$0$91009$39cecf19@news.twtelecom.net

On Fri, 15 Oct 2004 14:27:12 -0400, Matthew Heaney wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
> news:uv6ftd235dgf$.1px68p9wf1tjb.dlg@40tude.net...
>> On Mon, 04 Oct 2004 21:02:44 +1000, Brian May wrote:
>>
>> Then if one really needs a contravariant result, then Create should be
>> class-wide. It is as simple as:
>>
>>    function Create return X'Class;
> 
> The issue is not whether one needs a contravariant result.  Rather, the
> issue is whether the constructor is primitive for the type or not.

What do you mean here under "constructor"? A constructor in the sense this
word is used in C++ should be definitely primitive. Initialize in Ada is
also primitive, though it is not a full constructor in C++ sense.

> In general, constructors should *not* be primitive.

Why so? I buy a little inconvenience of overriding it in rare cases where
the base could do the job.

BTW, when and if Ada will have true user-defined constructors, then I think
we should seriously consider a requirement to override them in each derived
type, in all non-trivial cases, like when an uninitialized component is
added, or representation is changed etc. Even better would be to require
this always and provide a syntactical sugar for *explicit* reuse of the
base constructor.

> One way to do that is
> to declare the constructor in a nested package:
>
> package P is
>    type X is tagged ...;
>    package Constructors is
>       function Create return X;
>    end;
> end P;
>
> A constructor should always return a specific type, not a class-wide type.
> Your example above is wrong, since it would require the declaration of an
> object whose type is class-wide, and hence force the use of dispatching.

Not necessarily:

Object : X := X (Create); -- This is OK

> Clearly that's wrong, when you know the specific type of object you want to
> create.

I can always rename to a specific view when I know it:

Object : X'Class := Create;
I_know_you : X renames X (Object);

> The only time is makes sense for a constructor to return a class-wide type
> is when this is a factory method pattern.

Yes

>  In that case, the operation is
> primitive for parameter type:
> 
> package Q is
>    type X is abstract tagged ...;
>    type Y is abstract tagged ...;
> 
>    function Create (O : X) return Y'Class is abstract;

That isn't a factory, that should be multiple dispatch, I bet:

   function Create (O : X) return Y is abstract; -- Illegal in Ada

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



      reply	other threads:[~2004-10-16 19:25 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-02 15:39 Dynamich Dispatching Rick Santa-Cruz
2004-10-03 17:11 ` Martin Krischik
2004-10-03 22:59   ` Brian May
2004-10-04  8:22     ` Martin Krischik
2004-10-03 18:56 ` Ludovic Brenta
2004-10-04  4:45   ` Jeffrey Carter
2004-10-04 21:01     ` Ludovic Brenta
2004-10-05  0:32       ` Jeffrey Carter
2004-10-04  8:02 ` Dmitry A. Kazakov
2004-10-04 11:02   ` Brian May
2004-10-04 12:50     ` Dmitry A. Kazakov
2004-10-15 18:27       ` Matthew Heaney
2004-10-16 19:25         ` Dmitry A. Kazakov [this message]
replies disabled

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