comp.lang.ada
 help / color / mirror / Atom feed
From: mfeldman@seas.gwu.edu (Michael Feldman)
Subject: Re: Constructor in ADA9X
Date: 10 Feb 1995 17:38:54 -0500
Date: 1995-02-10T17:38:54-05:00	[thread overview]
Message-ID: <3hgptu$qe5@felix.seas.gwu.edu> (raw)
In-Reply-To: RTOAL.13.0010EBFD@lmumail.lmu.edu

In article <RTOAL.13.0010EBFD@lmumail.lmu.edu>,
Ray Toal <RTOAL@lmumail.lmu.edu> 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/
------------------------------------------------------------------------



  parent reply	other threads:[~1995-02-10 22:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-02-06 20:40 Constructor in ADA9X DELROEUX JEAN-YVES
1995-02-06 23:35 ` Tore Joergensen
1995-02-08  6:09   ` Kenneth Almquist
1995-02-08 16:55   ` Ray Toal
1995-02-10 13:49     ` Robert A Duff
1995-02-11 11:46       ` Stephane Barbey
1995-02-10 22:38     ` Michael Feldman [this message]
1995-02-08 14:42 ` Tucker Taft
1995-02-10  0:22   ` Robert I. Eachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox