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: a07f3367d7,59f8c74007a6b12b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!h2g2000vbd.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Forcing use of "constructors" Date: Mon, 23 Nov 2009 05:38:03 -0800 (PST) Organization: http://groups.google.com Message-ID: <27db03bc-9655-4a65-a730-bcad83bfe616@h2g2000vbd.googlegroups.com> References: <15055d2d-0dd5-4c84-bd4a-1cc60e9dea2d@p32g2000vbi.googlegroups.com> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1258983483 21372 127.0.0.1 (23 Nov 2009 13:38:03 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 23 Nov 2009 13:38:03 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: h2g2000vbd.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:8210 Date: 2009-11-23T05:38:03-08:00 List-Id: xorque wrote: > Hello, all. > > What's the preferred way to mandate that objects be created by > the use of given "constructor" subprograms? > > By this, I mean that I want to create a type and the only way to > get new values of this type is to use a subprogram that I provide. > > * It's a record type. > * A few of the components must be set to "sensible" values before > =A0 use. > * What constitutes a "sensible" value isn't known until runtime. > * I don't particularly want to add "is initialized" checks on all > =A0 subprograms in the package. > > Regards, > xw Declare your type to have unknown discriminants: package P is type T (<>) -- unknown discriminants: objects requires initialization is [limited] private; function Construct return T; private type T is [limited] record ... end record; end P; If the type is limited, the only way for a client to create an object is to call Construct. If the type is nonlimited, the client has two ways: call Construct or copy an existing object to a new one. Inside Construct, you would normally use the extended return statement (ARM 6.5(2.1/2)), e.g. function Construct return T is begin return Object : T do -- initialize some components end return; end Construct; You can also make your type controlled or limited controlled. You can expose the controlledness in the public part or hide it in the private part. -- Ludovic Brenta;