comp.lang.ada
 help / color / mirror / Atom feed
* Constructor in ADA9X
@ 1995-02-06 20:40 DELROEUX JEAN-YVES
  1995-02-06 23:35 ` Tore Joergensen
  1995-02-08 14:42 ` Tucker Taft
  0 siblings, 2 replies; 9+ messages in thread
From: DELROEUX JEAN-YVES @ 1995-02-06 20:40 UTC (permalink / raw)


 I would like to know how make a constructor ( like in C++ ) in ADA9X. Is it
possible ?? If yes how and if not, why and how can I simulate constructors ?

	Thanks for your help.

	DELROEUX Jean - Yves



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  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-08 14:42 ` Tucker Taft
  1 sibling, 2 replies; 9+ messages in thread
From: Tore Joergensen @ 1995-02-06 23:35 UTC (permalink / raw)


DELROEUX JEAN-YVES (jydelr@vub.ac.be) wrote:
:  I would like to know how make a constructor ( like in C++ ) in ADA9X. Is it
: possible ?? If yes how and if not, why and how can I simulate constructors ?

: 	Thanks for your help.

: 	DELROEUX Jean - Yves

From the Ada9x-RefManual's index:
constructor 
      See initialization 3.3.1(19), 7.6(1) 
      See initialization expression 3.3.1(4) 
      See Initialize 7.6(1) 
      See initialized alligator 4.8(4) 
You can find the ref.manual with a web-browser on
http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/

--
______________________________________________________________________
Tore B. Joergensen,    |    e-mail:     tore@lis.pitt.edu
a norwegian student    |    snail-mail: 2201 Pittockstr.
a long way from home.  |                Pittsburgh, 15217 PA
                       |    web:        http://www.pitt.edu/~tojst1



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  1995-02-06 23:35 ` Tore Joergensen
@ 1995-02-08  6:09   ` Kenneth Almquist
  1995-02-08 16:55   ` Ray Toal
  1 sibling, 0 replies; 9+ messages in thread
From: Kenneth Almquist @ 1995-02-08  6:09 UTC (permalink / raw)


> From the Ada9x-RefManual's index:
> constructor 
>       See initialization 3.3.1(19), 7.6(1) 
>       See initialization expression 3.3.1(4) 
>       See Initialize 7.6(1) 
>       See initialized alligator 4.8(4) 

If you are careless with initialized alligators, you could lose a hand
or a foot.  Talk about unsafe programming language contructs!

The index lists alligators under "constructor" and "heap managment."
It would seem more appropriate to list them under "destructor,"
"protected body," and "swamp management."
				Kenneth Almquist



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  1995-02-06 20:40 Constructor in ADA9X DELROEUX JEAN-YVES
  1995-02-06 23:35 ` Tore Joergensen
@ 1995-02-08 14:42 ` Tucker Taft
  1995-02-10  0:22   ` Robert I. Eachus
  1 sibling, 1 reply; 9+ messages in thread
From: Tucker Taft @ 1995-02-08 14:42 UTC (permalink / raw)


DELROEUX JEAN-YVES (jydelr@vub.ac.be) wrote:
:  I would like to know how make a constructor ( like in C++ ) in ADA9X. Is it
: possible ?? If yes how and if not, why and how can I simulate constructors ?

The only "predefined" constructor in Ada 95 is the aggregate
(array, record, and extension aggregates are provided).
All other constructors are simply user-written functions.
Any function that returns T or access-to-T (or T'Class or 
access-to-T'Class) can be considered a constructor.

Since aggregates are only usable when a type's full definition
is visible, only the user-defined "constructors" are
available to clients of a private type.  These user-defined
constructors are presumably implemented internally using
an aggregate (or simply a series of assignments).

Note that some constructors in C++ are used to perform
default initialization.  This is provided in Ada 95
through normal record component defaults (as in Ada 83)
plus an optional user-defined Initialize procedure
for "controlled" types (those descended from 
Finalization.[Limited_]Controlled).  Because 
record component defaults are generally sufficient for
default initialization, an explicit Initialize procedure is 
not often needed.

Other constructors in C++ are used as "copy" constructors.
This facility is provided in Ada 95 via the Adjust procedure
for controlled types.

While I am at it -- destructors are provided via the Finalize
procedure for controlled types.

One last point -- sometimes you don't want a constructor
to be inherited, because the set of parameters it takes
is dependent on the specific type for which it was designed.
If you declare the constructor in the same package as the
associate type T, and it returns the type T, then it *will*
be inherited.  One nice way to avoid undesired inheritance for
certain constructors is to put all such constructors in 
a "child" package of the package where the original type
is defined.  

For example, if you have a type T declared in a package 
P, then such constructors could be declared in a child 
such as "P.Constructors" or "P.Factory".  Since, typically, fewer 
clients use constructors than use other operations on a type, this 
separation corresponds to a natural dividing line in the clients of an
abstraction.  Furthermore, it means you can add or modify the
set of constructors without disturbing code that only manipulates
already-constructed objects.  You can also partition the
constructors into multiple groups by creating multiple child
"constructor" packages, if that makes sense for a given
abstraction.

: 	Thanks for your help.

: 	DELROEUX Jean - Yves

-Tucker Taft   stt@inmet.com
Intermetrics, Inc.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  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-10 22:38     ` Michael Feldman
  1 sibling, 2 replies; 9+ messages in thread
From: Ray Toal @ 1995-02-08 16:55 UTC (permalink / raw)


In article <3h6bno$7q8@toads.pgh.pa.us> tore@lis.pitt.edu (Tore Joergensen) writes:
>Path: news.lmu.edu!news.mic.ucla.edu!library.ucla.edu!agate!howland.reston.ans.net!news.cac.psu.edu!news.pop.psu.edu!hudson.lm.com!godot.cc.duq.edu!toads.pgh.pa.us!tore
>From: tore@lis.pitt.edu (Tore Joergensen)
>Newsgroups: comp.lang.ada
>Subject: Re: Constructor in ADA9X
>Date: 6 Feb 1995 23:35:20 GMT
>Organization: University of Pittsburgh
>Lines: 23
>Message-ID: <3h6bno$7q8@toads.pgh.pa.us>
>References: <3h61fh$92n@rc1.vub.ac.be>
>NNTP-Posting-Host: shrike.lis.pitt.edu
>X-Newsreader: TIN [version 1.2 PL2]


>DELROEUX JEAN-YVES (jydelr@vub.ac.be) wrote:
>:  I would like to know how make a constructor ( like in C++ ) in ADA9X. Is it
>: possible ?? If yes how and if not, why and how can I simulate constructors ?

>:       Thanks for your help.

>:       DELROEUX Jean - Yves

>From the Ada9x-RefManual's index:
>constructor 
>      See initialization 3.3.1(19), 7.6(1) 
>      See initialization expression 3.3.1(4) 
>      See Initialize 7.6(1) 
>      See initialized alligator 4.8(4) 
>You can find the ref.manual with a web-browser on
>http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/

One thing about C++ constructors that I haven't really been able to
simulate are the fact that they are NOT inherited in C++ (which is
useful.)  Consider:

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?

Ray Toal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  1995-02-08 14:42 ` Tucker Taft
@ 1995-02-10  0:22   ` Robert I. Eachus
  0 siblings, 0 replies; 9+ messages in thread
From: Robert I. Eachus @ 1995-02-10  0:22 UTC (permalink / raw)


In article <D3orIs.62t@inmet.camb.inmet.com> stt@henning.camb.inmet.com (Tucker Taft) writes:

  > One last point -- sometimes you don't want a constructor
  > to be inherited, because the set of parameters it takes
  > is dependent on the specific type for which it was designed.
  > If you declare the constructor in the same package as the
  > associate type T, and it returns the type T, then it *will*
  > be inherited.  One nice way to avoid undesired inheritance for
  > certain constructors is to put all such constructors in 
  > a "child" package of the package where the original type
  > is defined.  

   What Tucker said is perfectly correct, but may be misleading to
novices.  If a function has a controlling result (of a tagged type),
then it must be overridden when inherited by a non-abstract type.

   (Hmmm...  That is also pretty confusing.  What Tucker is really
saying is that in general in Ada 9X, you usually don't want to declare
constructors where they will be inherited, because the person who
declares a type derived from it may be forced to define a useless or
"junk" constructor.  You can have inherited constructors if you want,
but usually you don't want.)

   
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  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
  1 sibling, 1 reply; 9+ messages in thread
From: Robert A Duff @ 1995-02-10 13:49 UTC (permalink / raw)


In article <RTOAL.13.0010EBFD@lmumail.lmu.edu>,
Ray Toal <RTOAL@lmumail.lmu.edu> wrote:
>  type Rectangle is new Shape with private;
>  function Make_Rectangle (R: in out Rectangle; Width, Height: Real)
>                                        return Rectangle;

I don't understand why Make_Rectangle takes a Rectangle as a parameter,
and also returns one as a result.  Don't you mean something like this
instead:

    function Make_Rectangle (Width, Height: Real) return Rectangle;

If that's what you meant, then you could make it class-wide:

    function Make_Rectangle (Width, Height: Real) return Rectangle'Class;

This function will not be inherited.

Alternatively, you can call all your constructors the same name (say,
"Make") -- then it won't do any harm that they get inherited.

- Bob



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  1995-02-08 16:55   ` Ray Toal
  1995-02-10 13:49     ` Robert A Duff
@ 1995-02-10 22:38     ` Michael Feldman
  1 sibling, 0 replies; 9+ messages in thread
From: Michael Feldman @ 1995-02-10 22:38 UTC (permalink / raw)


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/
------------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Constructor in ADA9X
  1995-02-10 13:49     ` Robert A Duff
@ 1995-02-11 11:46       ` Stephane Barbey
  0 siblings, 0 replies; 9+ messages in thread
From: Stephane Barbey @ 1995-02-11 11:46 UTC (permalink / raw)


In article <D3sEE1.70B@world.std.com>, bobduff@world.std.com (Robert A Duff) writes:
: Alternatively, you can call all your constructors the same name (say,
: "Make") -- then it won't do any harm that they get inherited.
: 
: - Bob

Actually, this last solution is usually not suitable, since the
parameters of the constructors do usually differ from the parent to the
derived type.

-Stephane
--
Stephane Barbey				"It's not easy getting real wings"	
INJ-335, barbey@di.epfl.ch



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~1995-02-11 11:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
1995-02-08 14:42 ` Tucker Taft
1995-02-10  0:22   ` Robert I. Eachus

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