comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org>
Subject: Re: how to import a package
Date: Wed, 06 Dec 2006 19:47:38 GMT
Date: 2006-12-06T19:47:38+00:00	[thread overview]
Message-ID: <u7Fdh.171644$aJ.77331@attbi_s21> (raw)
In-Reply-To: <1165371252.358817.57840@80g2000cwy.googlegroups.com>

markww wrote:
> Hi,
> 
> I have this snippet of code (from members here) which defines a generic
> type:
> 
>     generic
>     type T is private;
>         package P is
>     type Node;
>     type Node_Ptr is access Node;
>         type Node is record
>             Data     : T;
>             Prev_Rec : access Node; -- points to the next record or
> null if none exists.
>             Next_Rec : access Node; -- points to the previous record or
> null if none exists.
>         end record;
>     end P;
> 
> Now my compiler (gnat) complains that Node, and Node_Ptr are not seen
> by the rest of the application. Is this because they are scoped within
> package P?

Package are very basic to Ada. 1st you must know how to make use of a 
package. If you don't know that, you're not ready to do anything useful 
with Ada. In general, if you have a library-level package P, you make it 
visible to another compilation unit through a context clause:

with P;
package Q is
    ...

In this case, you have a generic package, which is not a package but a 
way to create a package tailored to your needs. You can only mention a 
generic package in a context clause or instantiation. You still have to 
mention it in a context clause:

with P;
procedure My_Project is

then you have to use it to make a new package (instantiation):

package X is new P (T => Person_Rec);

Now the things declared in P are available in X. You have to use a 
qualified name to access them: X. Node. (Since you're a beginner, I'd 
recommend avoiding the use clause until you have a better idea how these 
things work.)

The above is for the most common case that P is a library-level generic 
package. In your case it appears your generic package is declared in 
your main procedure (which is not very useful), so it is not 
library-level and is directly visible; you neither need nor can use a 
context clause for P.

But all this is very basic. You should work through a tutorial or text 
and understand packages and generic packages before trying this; both 
are available at adapower.com or adaworld.com.

It would also help if you used basic Ada indentation standards:

generic
    type T is private;
package P is
    type Node;
    ...
end P;

Again, by the time you've worked through a tutorial or text you should 
have been exposed to a bunch of examples, and will hopefully have seen 
the logic behind the common indentation approach.

If you've already been through a tutorial or text, including generics, 
then there's something you didn't understand. In that case, I'm not sure 
which part you didn't understand.

-- 
Jeff Carter
"People called Romanes, they go the house?"
Monty Python's Life of Brian
79



  parent reply	other threads:[~2006-12-06 19:47 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-06  2:14 how to import a package markww
2006-12-06  3:06 ` Adam Beneschan
2006-12-06  3:34   ` markww
2006-12-06  9:18     ` Simon Wright
2006-12-06 19:47 ` Jeffrey R. Carter [this message]
2006-12-06 23:56   ` markww
2006-12-07  1:18     ` Björn Persson
2006-12-07  1:26     ` Brian May
2006-12-07  4:14       ` markww
2006-12-07  4:40         ` Brian May
2006-12-07  9:32           ` Stuart
2006-12-07 11:21             ` Jean-Pierre Rosen
2006-12-11  6:16               ` markww
2006-12-11  6:50                 ` markww
2006-12-11  9:40                   ` Georg Bauhaus
2006-12-11 14:19                     ` markww
2006-12-11 15:03                       ` Dmitry A. Kazakov
2006-12-11 16:22                       ` Adam Beneschan
2006-12-11 20:28                       ` Jeffrey R. Carter
2006-12-12  3:19                         ` markww
2006-12-12  3:31                           ` Jeffrey R. Carter
2006-12-12  9:03                           ` Stuart
2006-12-12 10:56                           ` Georg Bauhaus
2006-12-11  7:00                 ` Simon Wright
2006-12-07  4:06     ` Jeffrey R. Carter
replies disabled

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