comp.lang.ada
 help / color / mirror / Atom feed
* Re: Using Controlled type objects.
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Andre Spiegel @ 1996-08-01  0:00 UTC (permalink / raw)



Andrea Coccoli writes:

> When I compile it, I receive this message:
> genstack.ads:#: controlled type must be declared at the library level 

The problem is that you are trying to instantiate the generic inside
your main procedure -- and hence, the controlled type is not at the
library level.  The error message is a bit misleading, because it is
reported for the declaration within the generic package.  But that one
obviously *is* at the library level.  [I think I've heard rumours that
the GNAT team wants to enhance that error message, maybe they already
did so, are you using the current version of GNAT?]

Anyway, the solution is to instantiate the generic separately, like

    package My_Stack is new Reusable (...)

as a compilation unit on its own, and then "with" that unit into your
main procedure.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Using Controlled type objects.
@ 1996-08-01  0:00 Andrea Coccoli
  1996-08-01  0:00 ` Andre Spiegel
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrea Coccoli @ 1996-08-01  0:00 UTC (permalink / raw)



Hi everybody,
I'm implementing a recoverable object in Ada95.
For the stack, I'm using the generic one provided by the Lovelace Tutor

GENSTACK.ADS

  with Ada.Finalization; use Ada.Finalization;

  generic
    type Item is private;  
  package GenStack is
    -- This implements a simple generic stack of Items.
    -- (C) 1996 David A. Wheeler.

    type Stack is new Controlled with private;
		      #	
    type Stack_Access is access all Stack'Class;
    function ..
    ...
 
  private 
    type Stack_Node;
    type Stack_Node_Access is access Stack_Node;
    type Stack is new Controlled with record
		      #
          Start : Stack_Node_Access;
        end record;
    procedure Adjust(Object : in out Stack);
    procedure Finalize(Object : in out Stack);
  end Generic_Stack;


GENSTACK.ADB

 package body GenStack is

    type Stack_Node is record
           Data : Item;
           Next : Stack_Node_Access;
        end record;
....

What I want to do is to create an object which could give me the chances
to save and to restore its state. My first idea was to have a generic
package, in order to achieve reusability. So I defined:

REUSABLE.ADS

with genstack;
generic

type item is private;

package reusable is

        package lstack is new genstack(Item);
        --use lstack;
        type local is tagged limited private; 
	...

private
        type local is tagged limited
                record
                state : Item;
                stc : lstack.stack;
                end record;
	...

The problems arise when I want to declare an instantiation of this
package:

EXAMPLE.ADB

with reusable;
procedure example is

	package myreusable is new reusable(character);
	...

When I compile it, I receive this message:
genstack.ads:#: controlled type must be declared at the library level 

(I've marked with # the point indicated by the compiler).

The question is: how can I keep on using the same structure and the
controlled type without such an error message?
Or, is there another solution?
I hope I didn't annoy you with silly questions,

Cheers,

	Andrea




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using Controlled type objects.
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 1996-08-01  0:00 UTC (permalink / raw)



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.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using Controlled type objects.
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Pascal Ledru @ 1996-08-01  0:00 UTC (permalink / raw)



Try

with reusable;
package example is

  package myreusable is new reusable(character);

end example;

see RM95 3.9.1 for acessibility rules discussion.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1996-08-01  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox