comp.lang.ada
 help / color / mirror / Atom feed
From: "markww" <markww@gmail.com>
Subject: Re: how to import a package
Date: 6 Dec 2006 15:56:36 -0800
Date: 2006-12-06T15:56:36-08:00	[thread overview]
Message-ID: <1165449396.112251.129200@l12g2000cwl.googlegroups.com> (raw)
In-Reply-To: <u7Fdh.171644$aJ.77331@attbi_s21>

Yes I'm still confused - sorry - I'm coming from C++ / java and the ada
syntax / scoping is a bit strange to me. So I thought a package is
equivalent to a structure/class.

I thought that the following line:

    package Person_List in new P (T => PERSON_REC)

is just instantiating one instance of the package called Person_List.
So the line:

    Start : Person_List.Node_Ptr;

doesn't make sense to me since it looks like 'Start' is being defined
as pointing to Person_List.Node_Ptr, when I thought the right side of
the colon must be a data type.

In any event, I can't event get past adding the following line under
the package:

     package Person_List is new P (T => PERSON_REC);

my compiler (gnat) gives an error saying:

    operator for type "Node_Ptr" defined at line 23, nstance at line 39
is not directly visible
    use clause would make operation legal

line 39 is the line I just listed above. Line 23 is where Node_Ptr is
defined in the package. So ok, I stick the line:

    use Person_List;

underneath 'package Person_List is new P (T => PERSON_REC); ' but the
compiler still throws the same error.

Thanks,
Mark



Jeffrey R. Carter wrote:
> 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




  reply	other threads:[~2006-12-06 23:56 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
2006-12-06 23:56   ` markww [this message]
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