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!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!cern.ch!news From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Constructing an object Date: Fri, 23 Sep 2005 10:23:02 +0200 Organization: CERN - European Laboratory for Particle Physics Message-ID: References: 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 1127463782 16940 (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: Xref: g2news1.google.com comp.lang.ada:5055 Date: 2005-09-23T10:23:02+02:00 List-Id: Hi, Matthew Heaney wrote: > But that's because you didn't declare the type in such a way that > initialization is enforced. You have a few possibilities, including > declaring the type as private, Is it really enough? Consider this: -- file shapes.ads package Shapes is type Color is (Black, White); type Shape is private; -- or tagged private function New_Shape(C : in Color) return Shape; private type Shape is record -- or tagged C : Color; end record; end Shapes; -- file shapes.adb package body Shapes is function New_Shape(C : in Color) return Shape is begin return Shape'(C => C); -- I like this syntax :) end New_Shape; end Shapes; -- file hello.adb with Shapes; procedure Hello is S : Shapes.Shape; -- oops, uninitialized Shape begin null; end Hello; It compiles fine (GNAT) and it allows me to declare an uninitialized object of private type Shape. The only thing that private type gives me is that I cannot tinker with its internals directly. This is good in itself, but not enough to save me from having uninitialized objects. I think that the most general approach is with abstract types, although at the same time it looks like an overkill in simpler cases and not without its own problems. In particular, it introduces an artifical hierarchy of types (not counting the cases where the hierarchy already exists at the design time, like with true Shapes, Animals, etc.). I miss constructors. The "real" ones. ;-) (no flame intended) > declaring it as private and with an > unknown discriminant, declaring it with a known discriminant, or giving > the record components default values. -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/