From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4a5bab72e3ac47fc X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Dynamich Dispatching... Date: Sat, 16 Oct 2004 21:25:53 +0200 Organization: cbb software GmbH Message-ID: <6ne5n2fgwz1x.a4ecpxmxmlr9.dlg@40tude.net> References: <18qpcb693v2zz$.1es3pcrwk4eiu$.dlg@40tude.net> <417013a9$0$91009$39cecf19@news.twtelecom.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de NHgU5e7oM+w18qsN+rUJSwS0MSHC7DAP513PoYHSufDsEnDsk= User-Agent: 40tude_Dialog/2.0.12.1 Xref: g2news1.google.com comp.lang.ada:5335 Date: 2004-10-16T21:25:53+02:00 List-Id: On Fri, 15 Oct 2004 14:27:12 -0400, Matthew Heaney wrote: > "Dmitry A. Kazakov" 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