comp.lang.ada
 help / color / mirror / Atom feed
* Re: Constructors?
       [not found] <35DF0616.77764632@tech.swh.lv>
@ 1998-08-25  0:00 ` Frank Ecke
  1998-08-25  0:00   ` Constructors? Tucker Taft
  1998-08-27  0:00   ` Constructors? Matthew Heaney
  0 siblings, 2 replies; 3+ messages in thread
From: Frank Ecke @ 1998-08-25  0:00 UTC (permalink / raw)


On Sat, 22 Aug 1998 20:55:34 +0300, Maxim Senin <maks@tech.swh.lv> wrote:

>I'm new to ADA. I'm looking for tips/tricks on how to write constructors. As I
>understand, in ADA I have to rewrite constructor completely for each subclass.
>How can I reduce code duplication? Can superclass constructor be called from
>descendent class constructor?

Use the name Ada, please.

Yes, you can call a superclass constructor from a descendent class constructor.
But beware, although the term constructor exists in Ada, it should not be
confused with the notion used in OO.  For example, in Java, constructors are
not inherited by the subclass.  In Ada, a constructor is simply a primitive
operation of a tagged type and, therefore, it is propagated into the subclass.
Consider, for illustration, a simple hierarchy of geometrical figures (Point,
Circle, Rectangle, etc.):

type Colored_Point is tagged limited record
   This_X : Float := 0.0;
   This_Y : Float := 0.0;
   This_Color : Color_Type := Black;
end record;


type Rectangle is new Colored_Point with record
   This_Length, This_Width : Natural := 1;
end record;


procedure Init_New_Colored_Point(
  This_Figure : in out Colored_Point;
  Init_X, Init_Y : Float := 0.0;
  Init_Color : Color_Type := Black) is

begin
   Set_XY(This_Figure, Init_X, Init_Y);
   Set_Color(This_Figure, Init_Color);
end Init_New_Colored_Point;


procedure Init_New_Rectangle(
  This_Figure : in out Rectangle;
  Init_X, Init_Y : Float := 0.0;
  Init_Color : Color_Type := Black;
  Init_Length, Init_Width : Natural := 1) is

begin
   Init_New_Colored_Point(
     Colored_Point(This_Figure),  -- effectively ignore the Rectangle
                                  -- components
     Init_X, Init_Y,
     Init_Color);  -- calls the ``constructor'' of the superclass
   This_Figure.This_Length := Init_Length;
   This_Figure.This_Width := Init_Width;
end Init_New_Rectangle;


Note that since Init_New_Colored_Point is inherited by Rectangle, there is the
risk of mixing up the various constructor calls.  A bit of care is needed.


Hope this helps.


Regards,


Frank




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

* Re: Constructors?
  1998-08-25  0:00 ` Constructors? Frank Ecke
@ 1998-08-25  0:00   ` Tucker Taft
  1998-08-27  0:00   ` Constructors? Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Tucker Taft @ 1998-08-25  0:00 UTC (permalink / raw)


Frank Ecke (franke@minet.uni-jena.de) wrote:

: ...
: But beware, although the term constructor exists in Ada, it should not be
: confused with the notion used in OO.  For example, in Java, constructors are
: not inherited by the subclass.  In Ada, a constructor is simply a primitive
: operation of a tagged type and, therefore, it is propagated into the subclass.
: Consider, for illustration, a simple hierarchy of geometrical figures (Point,
: Circle, Rectangle, etc.):
: ...

This is not the only way to do things.  The term "constructor" is not defined 
in the Ada reference manual.  The closest thing to a built-in "constructor" 
in Ada is an aggregate.  A user-defined constructor is just a function
or procedure which creates and/or initializes an object.  It might
or might not be a primitive operation of the type, and the type might
or might not be tagged.

There are various coding conventions that have been developed for defining
constructor-like operations.  Some of these use primitive operations, 
others use operations that are specifically *not* primitive operations
because of a desire that they *not* be inherited.  The easiest
way to declare an operation which is not primitive, and hence not
inherited, is to declare it in a sub-package or child package.

I personally like the idea of putting constructors in a child
package because of my experience that only a subset of the clients
of an abstraction create new instances of the abstraction.  A much
larger subset of the clients simply manipulate existing instances.
Furthermore (in my experience), adding new, possibly specialized constructors
is a more frequent operation than adding other kinds of primitive operations.
By putting the constructors in a child package (or multiple child packages)
most of the clients are isolated from changes to constructors.

Finally, rather than explicit "constructors," the design pattern called
a "factory" is often a preferable approach.  Factories hide from the
caller which particular type of object is created, and simply promise
that its type is derived from some particular (usually abstract) type.
That is often all you need to know in an OO language, especially when
manipulating most objects by reference (i.e., using access values).

: Frank

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




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

* Re: Constructors?
  1998-08-25  0:00 ` Constructors? Frank Ecke
  1998-08-25  0:00   ` Constructors? Tucker Taft
@ 1998-08-27  0:00   ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1998-08-27  0:00 UTC (permalink / raw)


franke@minet.uni-jena.de (Frank Ecke) writes:

> Yes, you can call a superclass constructor from a descendent class
> constructor.  But beware, although the term constructor exists in Ada,
> it should not be confused with the notion used in OO.  For example, in
> Java, constructors are not inherited by the subclass.  In Ada, a
> constructor is simply a primitive operation of a tagged type and,
> therefore, it is propagated into the subclass.

Not necessarily.  In fact, the usual rule of thumb is declare
constructors so that they are NOT primitive operations of the type.

You can do this be declaring the constructors in a nested package, or
just return the class-wide type as the return value.

One time when you do want to make the constructor primitive is when you
need it to dispatch, as in the case of a factory method.




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

end of thread, other threads:[~1998-08-27  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <35DF0616.77764632@tech.swh.lv>
1998-08-25  0:00 ` Constructors? Frank Ecke
1998-08-25  0:00   ` Constructors? Tucker Taft
1998-08-27  0:00   ` Constructors? Matthew Heaney

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