comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: premature use of private
Date: 1999/08/17
Date: 1999-08-17T00:00:00+00:00	[thread overview]
Message-ID: <37b9ac3d@news1.us.ibm.net> (raw)
In-Reply-To: In2u3.3680$z6.293053@typhoon-sf.snfc21.pbi.net

In article <In2u3.3680$z6.293053@typhoon-sf.snfc21.pbi.net> , tmoran@bix.com
wrote:

> So far so good.  But I don't want a Pile of mixed Fruit, so,
> borrowing from Barnes' on Containers, I tried:
>
>   generic
>     type This_Fruit is abstract tagged private;
>     type Fruit_Ptr is access all This_Fruit;
>   package Sets is
>     type Pile is array(integer range <>) of Fruit_Ptr;
>     procedure Sort_By_Size(X : in out Pile);
>   end Sets;
>
>   type Apple_Ptr is access all Apple;
>   package Apples is new Sets(Apple, Apple_Ptr);
>
> But that won't work because Apple is private at this point.
>
>   How should this be done?

In order to instantiate the package, you need the full view of the type.

I would declare the instantiations as children of the package in which the
type is declared, e.g.:

  package Fruit is

    type Fruit_Type is ...;

    type Apple_Type is new Fruit_Type with private;
    type Apple_Ptr is access all Apple_Type;
...
  end Fruit;

   generic
     type This_Fruit is abstract tagged private;

--or type This_Fruit is new Fruit_Type with private;

     type Fruit_Ptr is access all This_Fruit;

--or, rather than importing the access type as a generic formal type,
--just declare it locally, in the spec of Sets.

   package Fruit.Sets is
     type Pile is array(integer range <>) of Fruit_Ptr;
     procedure Sort_By_Size(X : in out Pile);
   end Fruit.Sets;


Now, for the instantiation:

  with Fruit.Sets;
  package Fruit.Apple_Sets is
    new Fruit_Sets (Apple_Type, Apple_Ptr);

If you go with the child package idea (suggested by David Hoos), then you
structure the hierarchy as:

  package Fruit is

    type Fruit_Type is abstract tagged private;
...
  end Fruit;

  package Fruit.Apples is

    type Apple_Type is new Fruit_Type with private;
    type Apple_Ptr is access all Apple_Type;  -- or Apple_Type'Class
...
  end Fruit.Apples;

  with Fruit.Sets;
  package Fruit.Apples.Sets is
    new Fruit.Sets (Apple_Type, Apple_Ptr);


You don't have to cram the entire world in one package -- break it up into a
package hierarchy.  The idea is to always submit as little program text to
the compiler as possible.

--
Matt

It is impossible to feel great confidence in a negative theory which has
always rested its main support on the weak points of its opponent.

Joseph Needham, "A Mechanistic Criticism of Vitalism"




  reply	other threads:[~1999-08-17  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-08-17  0:00 premature use of private tmoran
1999-08-17  0:00 ` Matthew Heaney [this message]
1999-08-17  0:00   ` tmoran
1999-08-17  0:00     ` David C. Hoos, Sr.
1999-08-18  0:00     ` Matthew Heaney
1999-08-18  0:00       ` tmoran
1999-08-18  0:00         ` Matthew Heaney
1999-08-17  0:00 ` David C. Hoos, Sr.
replies disabled

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