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, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,342dcd67e9ca73ee X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!newsfeed2.dallas1.level3.net!news.level3.com!bos-service1.raytheon.com!dfw-service2.ext.ray.com.POSTED!53ab2750!not-for-mail From: Jeffrey Carter User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: tagged record child: override constructor? References: <1126591134.797303.318920@z14g2000cwz.googlegroups.com> In-Reply-To: <1126591134.797303.318920@z14g2000cwz.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Tue, 13 Sep 2005 09:37:04 -0700 NNTP-Posting-Host: 147.24.111.90 X-Complaints-To: news@ext.ray.com X-Trace: dfw-service2.ext.ray.com 1126629425 147.24.111.90 (Tue, 13 Sep 2005 11:37:05 CDT) NNTP-Posting-Date: Tue, 13 Sep 2005 11:37:05 CDT Organization: Raytheon Company Xref: g2news1.google.com comp.lang.ada:4615 Date: 2005-09-13T09:37:04-07:00 List-Id: sean.gilbertson@gmail.com wrote: > I have a tagged record that is declared private, along with a > constructor function which returns an instance. I do this to enforce > the assignment of several required fields in the record, so if you know > how to do this another way, please let me know! It depends on what you mean by "enforce the assignment of several required fields". It is impossible to force the client to call your function, though you can arrange things so that the type is useless to the client if the client doesn't first call your function. If you have type T is private; function Make return T; then you don't need Make at all. You can ensure the required fields are initialized through default values: private type T is record X : Integer := -37; Y : Character := '*'; Z : Duration; end record; (Note that this is a property of record types in general; whether your record type is tagged doesn't affect this.) If you have fields you can't easily initialize through defaults, you can use the initialization features of controlled types: private type T is new Ada.Finalization.Controlled with record ... end record; procedure Initialize (Object : in out T); Initialize is called for every object of type T when it is created. On the other hand, if you mean the assignment of client-supplied values to the required components, your choices are more limited. If the values are such that they may be discriminants, then that's a way to force the client to supply them: type T (X : Integer) is private; If your required fields cannot be discriminants, then things get more complicated. You can include a Boolean field, default initialized to False, which is set to True by your function. All other operations of the type then check this field, and raise an exception if it is False: type T is private; procedure Initialize (X : out T; ...); Not_Initialized : exception; procedure Op (X : in out T); private type T is record Initialized : Boolean := False; ... end record; ... procedure Initialize (X : out T; ...) is ... begin -- Initialize X.Initialized := True; ... end Initialize; procedure Op (X : in out T) is ... begin -- Op if not X.Initialized then raise Not_Initialized; end if; ... end Op; This makes the type useless unless an object is first initialized. None of this has anything to do with the fact that if T is tagged and a primitive operation of T is a function that returns T, then, yes, you do have to override such functions for each child of T. -- Jeffrey Carter "Now go away or I shall taunt you a second time." Monty Python and the Holy Grail E-mail: jeffrey_r_carter-nr [commercial-at] raytheon [period | full stop] com