comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Child package: private type and IO
Date: 1998/02/27
Date: 1998-02-27T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680002702982024210001@news.ni.net> (raw)
In-Reply-To: 6cbuat$cg3$1@nnrp1.dejanews.com


In article <6cbuat$cg3$1@nnrp1.dejanews.com>, sparre@cats.nbi.dk wrote:


>A parent-client package example:
>
>   --  file: parent.ads
>
>   package Parent is
>
>      type Object is ...;
>
>      function Initialise return Object;
>
>   end Parent;

Note that this is NOT the way to organize this package if type Object is
tagged.  For a tagged type, constructors - defined as functions returning a
value of the type - should return the class-wide type, or should be
declared in a nested package, ie

package P is

   type T is tagged ...;

   package Constructors is

      function New_T return T;

   end;
...
end P;

-or-

package P is

   type T is tagged ...;

   function New_T return T'Class;
...
end P;


The problem is that very often you use the "transitivity of visibility"
technique to get direct visitibility to the type and its operations, with a
null extension, ie

   type NT is new P.T with null record;

but if T has primitive operations that are functions returning type T, then
type NT "goes abstract," and the poor client has to override them.  This is
NOT what you want to have happen.

By declaring the constructor in a nested package, then the function isn't
primitive anymore, and so it is not inherited.  Therefore, any derivation
doesn't go abstract, because there are not inheritable functions that
return the specific type.

The same effect may be achieved by making the return type class-wide.  Then
the function is no longer primitive, and so is not inherited during a
derivation.  Therefore, the derived type does not go abstract, which is the
behavior we want.

I'll also make an Ada style point.  Please do not name a type "type Object
is..."  This is VERY confusing, because an object is an object, not a type. 
Objects are the memory locations that store a value of a type.  So when
refer to an object's type as "Object," then how do you refer to the object?

A type should be named using a noun phrase, such as

type Text_File is  ...;

type Singly_Linked_List is ...;

type Storage_Element_Array is array ...;

type Ownship_Heading is ...;

type Integer is ...;

A good guideline is, Be consistant with the style in the reference manual. 
There are NO types in the RM named "Object," so there should be none in
your code either.




  reply	other threads:[~1998-02-27  0:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-02-14  0:00 Child package: private type and IO johnjohn
1998-02-16  0:00 ` Tom Moran
1998-02-17  0:00 ` sparre
1998-02-27  0:00   ` Matthew Heaney [this message]
1998-03-01  0:00     ` type names (was Re: Child package: private type and IO) Ray Blaak
1998-03-01  0:00       ` Matthew Heaney
1998-03-01  0:00         ` Brian Rogoff
1998-03-01  0:00           ` Matthew Heaney
1998-03-03  0:00             ` Ray Blaak
1998-03-04  0:00         ` Fergus Henderson
1998-03-03  0:00           ` Brian Rogoff
1998-03-04  0:00             ` John G. Volan
1998-03-05  0:00               ` Case sensitivity [was Re: type names (was Re: Child package: private type and IO)] Anonymous
1998-03-05  0:00                 ` John G. Volan
replies disabled

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