comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Using Controlled type objects.
Date: 1996/08/01
Date: 1996-08-01T00:00:00+00:00	[thread overview]
Message-ID: <DvGsz3.24v@world.std.com> (raw)
In-Reply-To: 32008E75.6A3F@ncl.ac.uk


In article <32008E75.6A3F@ncl.ac.uk>,
Andrea Coccoli  <A.M.Coccoli@ncl.ac.uk> wrote:
>genstack.ads:#: controlled type must be declared at the library level 

Always instantiate your generic at library level.  That is, make a
library package, and put the instantiation inside it.  Or, make the
instantiation itself a library package.  Do not instantiate your generic
inside a procedure.

You can then "with" the library package from your procedure Example.

- Bob

P.S. Lots of people seem to trip over this.  The problem, I think, is
that people think of the main subprogram as being "all there is", so
things declared in it ought to be library level (and I'm guessing that
you intended procedure Example to be the main subprogram).  But in Ada,
lots of things can happen after the main subprogram exits -- in
particular, library tasks keep running, and the main subprogram can be
recursive, or can be called from another task.  But lots of programs
don't have any tasks, and the only call to the main subprogram is the
automatic one that happens just after elaborating the library units, so
in *these* programs, it is true that locals of the main subprogram "live
forever".

The reason for the rule about controlled types is to prevent dangling
references, and since there *might* be tasks (etc), the rule has to
apply to the main subprogram just like any other subprogram.  (Actually,
the rule is not specific to controlled types -- it applies to any tagged
type -- it's illegal if the parent is less nested than the derived type
(where nesting in a package doesn't count).)  And the fact that the
controlled type is in a generic complicates the issue, because when you
compile the generic, the compiler doesn't know whether you intend to
make nested instantiations.  So the error message comes out when you
actually *do* the nested instantiation -- the compiler then notices that
the *instance* contains an evilly-nested controlled type.

Anyway, there's nothing wrong with your generic.  It just happens to be
the sort of generic that can't be instantiated within a procedure.




      parent reply	other threads:[~1996-08-01  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-08-01  0:00 Using Controlled type objects Andrea Coccoli
1996-08-01  0:00 ` Andre Spiegel
1996-08-01  0:00 ` Pascal Ledru
1996-08-01  0:00 ` Robert A Duff [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