comp.lang.ada
 help / color / mirror / Atom feed
* Procedure Access Values
@ 1998-02-03  0:00 Anton Gibbs
  0 siblings, 0 replies; 3+ messages in thread
From: Anton Gibbs @ 1998-02-03  0:00 UTC (permalink / raw)



Please can someone help me to understand a problem I have run into
using procedure access values.

We have a system that uses call-back procedures in a manner
similar to the following:-

package Notifiers is
   type Callback_Type is access procedure;
   procedure Notify_Me_When_Event_Occurs( Callback : in Callback_Type );
end Notifiers;

package P is
   procedure Start;
end P;

with Notifiers;
package body P is

   procedure Event_Handler is ...;
   
   procedure Start is
   begin
      Notifiers.Notify_Me_When_Event_Occurs( Event_Handler'Access );
   end Start;

end P;

This all works fine. The problem we have run into occurs when we
convert package P into a generic. ie:-

generic package P is
   procedure Start;
end P;

with Notifiers; 
package body P is
 
   procedure Event_Handler is ...;
   
   procedure Start is
   begin 
      Notifiers.Notify_Me_When_Event_Occurs( Event_Handler'Access );--*
   end Start;
 
end P;

Now we get the following compilation error on the line marked *:-

    "access type must not be outside generic body"

I realise that in Ada95 there are rules governing access values which
are intended to prevent dangling references. What baffles me is that
if the declaration of procedure Event_Handler is moved to the package
specification, then the compiler accepts the 'Access without complaint.

How can the legality of taking an access value depend on whether or
not the procedure is local ?

If anyone can shed any light on this I would be most grateful.

Thank you.

Anton Gibbs
Eurocontrol




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

* Procedure Access Values
@ 1998-02-12  0:00 Anton Gibbs
  1998-02-13  0:00 ` John English
  0 siblings, 1 reply; 3+ messages in thread
From: Anton Gibbs @ 1998-02-12  0:00 UTC (permalink / raw)



Please can someone help me to understand a problem I have run into
using procedure access values.

We have a system that uses call-back procedures in a manner
similar to the following:-

package Notifiers is
   type Callback_Type is access procedure;
   procedure Notify_Me_When_Event_Occurs( Callback : in Callback_Type );
end Notifiers;

package P is
   procedure Start;
end P;

with Notifiers;
package body P is

   procedure Event_Handler is ...;
   
   procedure Start is
   begin
      Notifiers.Notify_Me_When_Event_Occurs( Event_Handler'Access );
   end Start;

end P;

This all works fine. The problem we have run into occurs when we
convert package P into a generic. ie:-

generic package P is
   procedure Start;
end P;

with Notifiers; 
package body P is
 
   procedure Event_Handler is ...;
   
   procedure Start is
   begin 
      Notifiers.Notify_Me_When_Event_Occurs( Event_Handler'Access );--*
   end Start;
 
end P;

Now we get the following compilation error on the line marked *:-

    "access type must not be outside generic body"

I realise that in Ada95 there are rules governing access values which
are intended to prevent dangling references. What baffles me is that
if the declaration of procedure Event_Handler is moved to the package
specification, then the compiler accepts the 'Access without complaint.

How can the legality of taking an access value depend on whether or
not the procedure is local ?

If anyone can shed any light on this I would be most grateful.

Thank you.

Anton Gibbs
Eurocontrol




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

* Re: Procedure Access Values
  1998-02-12  0:00 Anton Gibbs
@ 1998-02-13  0:00 ` John English
  0 siblings, 0 replies; 3+ messages in thread
From: John English @ 1998-02-13  0:00 UTC (permalink / raw)



Anton Gibbs (agibbs@dera.gov.uk) wrote:
: This all works fine. The problem we have run into occurs when we
: convert package P into a generic. ie:-

: generic package P is
:    procedure Start;
: end P;

: with Notifiers; 
: package body P is
:    procedure Event_Handler is ...;
:    
:    procedure Start is
:    begin 
:       Notifiers.Notify_Me_When_Event_Occurs( Event_Handler'Access );--*
:    end Start;
:  
: end P;

This will be a problem if you instantiate your generic inside a procedure.
The elaboration of the package body happens at the point of instantiation,
i.e. one lexical level deeper than the level (library level) at which
Notifiers.Event_Handler is declared. The solution is to instantiate
the generic in a library level package, i.e.

  with P;
  package Q is
    package R is new P;  -- the access value in R is at the same level as P
  end Q;

  with Q;
  procedure Main is
    -- use Q.R here
  end Main;

rather than:

  with P;
  procedure Main is
    package R is new P;  -- R contains an access created at the wrong
    ...			 -- lexical level
  end Main;

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




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

end of thread, other threads:[~1998-02-13  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-02-03  0:00 Procedure Access Values Anton Gibbs
  -- strict thread matches above, loose matches on Subject: below --
1998-02-12  0:00 Anton Gibbs
1998-02-13  0:00 ` John English

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