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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,555956c1cdd22308 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Help - Constructors - ASAP. Date: 1998/08/01 Message-ID: #1/1 X-Deja-AN: 376885117 Sender: matt@mheaney.ni.net References: <6p75qi$rcj@news.latnet.lv> <6pi4jq$j73$1@nnrp1.dejanews.com> <6pqdr2$hn2$1@nnrp1.dejanews.com> <35C1043E.9FFB23D0@elca-matrix.ch> <6psvnb$vkt$1@nnrp1.dejanews.com> NNTP-Posting-Date: Fri, 31 Jul 1998 21:30:57 PDT Newsgroups: comp.lang.ada Date: 1998-08-01T00:00:00+00:00 List-Id: dennison@telepath.com writes: > But, a naieve reader would look at that and think that they could get > back an object of *any* type in the class (as I have just proven :-) > ), when in actuality they always get back the same class of object. If you don't like returning the class-wide type, then declare a constructor that returns the specific type, but is not primitive. The simplest way to do this is to declare the constructors in a nested package: package P is type T is tagged private; package Constructors is function New_T (I : Integer) return T; end Constructors; private ... end P; > If I read you correctly, what you were worried about was developers > screwing up and forgetting to override the default > constructors. Nothing about this solution prevents that. Now instead > of getting the default constructor, our hapless client will compile > with the class-wide constructor. They get an exception at runtime > either way. But now the client, who is just as capable of screwing up > as the subclass author, could goof and use the class-wide constructor > on his own. You can detect type mismatches at compile-time by using a constructor that returns the specific type (not the class-wide type), and is non-primitive. See the example above. > Hmm. I believe the language rules state that no more primitive > operations may be declared after the type is "finalized". It may be > possible to force this by doing something like deriving a dummy > subclass before the constructor is declared. I'm not sure what you mean. If you want non-primitive operations, then just declare them in a nested package. Don't use the freezing rules (see RM95 13.14) to try to do this; that would be confusing.