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,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!cern.ch!news From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Constructing an object Date: Wed, 21 Sep 2005 10:46:35 +0200 Organization: CERN - European Laboratory for Particle Physics Message-ID: 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 1127292395 15444 (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 Xref: g2news1.google.com comp.lang.ada:4988 Date: 2005-09-21T10:46:35+02:00 List-Id: Hi, I'm learning Ada and I would like to better understand the ways used to create objects. Let's say I have the following: type Color is (Black, Red, Green, Blue, White); type Shape is tagged record C : Color; end record; type Rectangle is new Shape with record A, B : Positive; end record; From what I've learnt ("Ada 95: The Craft of Object-Oriented Programming" by John English), there are two possible ways to implement "constructors" for these types: 1. By procedure, which is a primitive operation for each type in the hierarchy, for example: procedure New_Shape(C : in Color; S : out Shape) is begin S.C := C; end; procedure New_Rectangle(A, B : in Positive; C : in Color; R : out Rectangle) is begin New_Shape(C, Shape(R)); R.A := A; R.B := B; end; -- another version (btw - which version do you prefer?) procedure New_Rectangle(A, B : in Positive; C : in Color; R : out Rectangle) is St : Shape; begin New_Shape(C, St); R := (St with A => A, B => B); end; This does not please me much, because I can have uninitialized objects, which may not make sense in the program: R : Rectangle; 2. By function: function New_Shape(C : in Color) return Shape is S : Shape; begin S.C := C; return S; end; function New_Rectangle(A, B : in Positive; C : in Color) return Rectangle is R : Rectangle; begin Shape(R) := New_Shape(C); -- is this OK? R.A := A; R.B := B; return R; end; This does not please me for the same reason - no enforcement of proper initialization, if a given type makes no sense uninitialized. What approach do you actually use in the real (non-tutorial) code? Are there other techniques? (I'm aware of the controlled types, but I need to provide parameters for construction, which the special Initialize procedure does not have.) -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/