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,2e2db8edf2656165 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!atl-c02.usenetserver.com!news.usenetserver.com!atl-c04.usenetserver.com!news.usenetserver.com!news.mailgate.org!news-zh.switch.ch!switch.ch!cern.ch!news From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Constructing an object Date: Thu, 22 Sep 2005 09:28:12 +0200 Organization: CERN - European Laboratory for Particle Physics Message-ID: References: <1lw8oz33ao529.12ep5ay7rg4oc$.dlg@40tude.net> NNTP-Posting-Host: abpc10883.cern.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sunnews.cern.ch 1127374092 6809 (None) 137.138.37.241 X-Complaints-To: news@sunnews.cern.ch User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050913 Red Hat/1.7.10-1.4.2.SL3 X-Accept-Language: en-us, en In-Reply-To: <1lw8oz33ao529.12ep5ay7rg4oc$.dlg@40tude.net> Xref: g2news1.google.com comp.lang.ada:5022 Date: 2005-09-22T09:28:12+02:00 List-Id: Dmitry A. Kazakov wrote: >>I'm learning Ada and I would like to better understand the ways used to >>create objects. >>1. By procedure, which is a primitive operation for each type in the >>hierarchy, >>2. By function: > 3. The default values: OK, this solves some of the cases. In general, however, by "initialization" I mean performing some actions (not just inventing values), like acquire on external resources. > 4. Initialization enforcement via discriminants: > > 4.a. Discriminants as members: > > type Shape (C : Color) is tagged null record; > -- C has to be defined I think it has the same problem. > 4.b. Box-discriminants: > > type Shape (<>) is tagged private; > -- Shape objects have to be initialized > > private > type Shape is tagged record > C : Color; > end record; I will have to read more about this, thanks for pointing the direction. > 5. Abstract types with private descendants and a factory: > > type Abstract_Shape is abstract tagged null record; > function New_Shape (...) return Abstract_Shape'Class; > -- Factory is the only public way to create it > private > type Shape is new Abstract_Shape with ...; And I think this is the most general solution. In addition, the types that I'm crazy about initializing are those which can introduce a lot of problems when not well encapsulated. For the same reason it makes sense to make them private and carefully design their behavioral interface. The above scheme should work well for them. Thanks for response, -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/