comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Packages and visibility...
Date: 1997/03/28
Date: 1997-03-28T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680002803970004290001@news.ni.net> (raw)
In-Reply-To: 333AB81E.109B@linkabit.titan.com


In article <333AB81E.109B@linkabit.titan.com>, t_mjb@linkabit.titan.com wrote:

>I have a package 'foo' with a data type 'bar' and various functions and
>procedures that act upon 'bar' that I would like to place in a child
>package also named 'bar.'

I'm not sure what you want to do.  Do you want a child package of Foo named
Bar, as follows:

package Foo.Bar is

   type T is ...;

end Foo.Bar;

or, do you want a nested package:

package Foo is

   package Bar is
  
      type T is ...;

   end Bar;

end Foo;


>In the program I would like to access this as:
>
>with foo; use foo;
>procedure test is
> b: bar;
>begin
> bar.init(b);
>end test;

You must understand one Very Important Thing about programming in Ada:

   A type is not a module.

Yes, many current languages equate the two concepts, but in Ada, the type
and module are orthogonal language features.

In Ada, you should not name your package and type the same.  If you really
want the syntax suggested by your example, then don't program in Ada.

A type declaration and its primitive operations are declared in a package
specifically dedicated to that purpose.  Name the package the plural of the
type.  I also like to qualify the type name with an adjective.  For
example:

generic
   type Stack_Item is private;
package Stacks_G is

   type Root_Stack is abstract tagged private;

   procedure Push (Item : in        Stack_Item;
                                  On     : in out Root_Stack) is abstract;

   function Top (Stack : Root_Stack) return Stack_Item is abstract;

...

end Stacks_G;

A child package containing a derivation might look like:

package Stacks_G.Bounded_G is

   type Bounded_Stack is new Root_Stack with private;

...
end Stacks_G.Bounded_G;

The instantiations are as follows:

with Stacks_G;
package Integer_Stacks is new Stacks_G (Integer);


with Stacks_G.Bounded_G;
package Integer_Stacks.Bounded is new Integer_Stacks.Bounded_G;

Then use it as follows:

with Integer_Stacks.Bounded;
procedure Proc is
   The_Stack : Integer_Stacks.Bounded.Bounded_Stack;
   use Integer_Stacks.Bounded;
begin
   Push (5, On => The_Stack);
end;

Quelle langue!

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




      reply	other threads:[~1997-03-28  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-03-27  0:00 Packages and visibility Marc Bejerano
1997-03-28  0:00 ` Matthew Heaney [this message]
replies disabled

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