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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f9c7f7f00103ac6 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-02-10 14:38:54 PST Path: nntp.gmd.de!newsserver.jvnc.net!nntpserver.pppl.gov!princeton!rutgers!sgigate.sgi.com!enews.sgi.com!ames!tulane!news.tulane.edu!darwin.sura.net!gwu.edu!gwu.edu!not-for-mail From: mfeldman@seas.gwu.edu (Michael Feldman) Newsgroups: comp.lang.ada Subject: Re: Constructor in ADA9X Date: 10 Feb 1995 17:38:54 -0500 Organization: George Washington University Message-ID: <3hgptu$qe5@felix.seas.gwu.edu> References: <3h61fh$92n@rc1.vub.ac.be> <3h6bno$7q8@toads.pgh.pa.us> NNTP-Posting-Host: 128.164.9.3 Date: 1995-02-10T17:38:54-05:00 List-Id: In article , Ray Toal wrote: >package Shapes is > > type Shape is abstract tagged record; > procedure Display (S; Shape) is abstract; > > type Circle is new Shape with private; > function Make_Circle (C: in out Circle; Radius: Real) return Circle; > procedure Display (C: Circle); > > type Rectangle is new Shape with private; > function Make_Rectangle (R: in out Rectangle; Width, Height: Real) > return Rectangle; > procedure Display (R: Rectangle); > > type Square is new Rectangle with private; > function Make_Square (S: in out Square; Side_Length: Real) return Square; > procedure Display (S: Square); > > type Triangle is .... > >private > > ... > >end Shapes; > >You will get the error message "Make_Rectangle must be redeclared for >type Square..." Does anyone know a nice, clean workaround? I ran into this a few weeks ago. We can discuss Ada 95 rules, but let's try to put the situation in plain English. Then I'll offer a solution. - Make_Rectangle is being used as a constructor. - Declaring it just below the type declaration makes it a primitive operation. - Primitive operations are inherited by derivative types (Square, in your case). - You really don't want a constructor to be inherited, because the derivative's constructor will almost always have a different parameter profile from its parent. This conflicts with the idea that a primitive operation will be either inherited or overridden; overriding implies a new function with the _same_ parameter profile. - the question to ask is then "how can I _prevent_ a constructor from being inherited?" There are several Ada 95 solutions; the one I am using at the moment simply declares an inner package Constructors, which exports the constructors. In your case, package Shapes is -- declare types and operations that are sensibly inherited -- then package Constructors is function Make_Circle.. function Make_Square.. function Make_Rectangle.. end Constructors; private ... end Shapes; This solution was suggested to me by one of the GNAT folks, to whom I am greatly indebted. Others may propose other solutions; this is the one I like for simple cases because it doesn't confuse the inheritance question with other issues like access types, child packages, etc. This area of Ada 95 is exceedingly confusing to the uninitiated, and the discussions of it in the existing texts - Rationale, Barnes, Skansholm - are really weak and incomplete. Hope this is clear and that it helps; I await _clear_ descriptions of the other solutions. Mike Feldman ------------------------------------------------------------------------ Michael B. Feldman - chair, SIGAda Education Working Group Professor, Dept. of Electrical Engineering and Computer Science The George Washington University - Washington, DC 20052 USA 202-994-5919 (voice) - 202-994-0227 (fax) - mfeldman@seas.gwu.edu (Internet) ------------------------------------------------------------------------ One, two, three ways an underdog: Ada fan, Mac fan, Old Liberal Democrat ------------------------------------------------------------------------ Ada on the World-Wide Web: http://lglwww.epfl.ch/Ada/ ------------------------------------------------------------------------