comp.lang.ada
 help / color / mirror / Atom feed
* Using a procedure as a parameter (Gnat)
@ 1996-03-26  0:00 Chris O'Regan
  1996-03-27  0:00 ` Robert A Duff
  0 siblings, 1 reply; 2+ messages in thread
From: Chris O'Regan @ 1996-03-26  0:00 UTC (permalink / raw)



   I am writing a subprogram that accepts as one of its parameters the name
of another subprogram.  As described in "Rendezvous with Ada 95", I can
achieve this by defining the parameter as an access type that points to a
subprogram.  As a test, I typed in an example from the book and successfully
compiled it using gnat v3.03.  This is the source:

procedure Foo is
   Total: Integer;

   function P (I: in Integer) return Integer is
   begin
      return (I ** 2) + (5 * (I - 7));
   end;

   type Polynomial is access function (N: in Integer) return Integer;

   function Sum_Of_Terms (Poly : in Polynomial; From, To: in Integer)
			 return Integer is
      Sum: Integer := 0;
   begin
      for Term in From..To loop
	 Sum := Sum + Poly (Term);
      end loop;
      return Sum;
   end Sum_Of_Terms;
begin
   Total := Sum_Of_Terms (Poly => P'Access,
			  From => 3,
			  To => 8);
end Foo;


   I then re-wrote the example such that the subprogram was part of the
package and the subprogram being passed was defined in the main procedure. 
Unfortunately, when I try to compile the main procedure, it fails to build
the executable with this error:

$ gnatmake ack.adb
gcc -c ack.adb
ack.adb:11:35: subprogram must not be deeper than access type
gnatmake: *** compilation failed.


   Below are the source files:


ack.adb:
========
with Bar;
use Bar;
procedure Ack is
   Total: Integer;
   
   function P (I: in Integer) return Integer is
   begin
      return (I ** 2) + (5 * (I - 7));
   end;
begin
   Total := Sum_Of_Terms( Poly => P'Access, From => 3, To => 8 );
end Ack;

bar.ads:
========
package Bar is
   type Polynomial is access function ( N: in Integer ) return Integer;

   function Sum_Of_Terms( Poly: in Polynomial; From, To: in Integer )
			 return Integer;
end Bar;

bar.adb:
========
package body Bar is
   function Sum_Of_Terms( Poly: in Polynomial; From, To: in Integer )
                         return Integer is
      Sum: Integer := 0;
   begin
      for Term in From..To loop
	 Sum := Sum + Poly (Term);
      end loop;
      return Sum;
   end Sum_Of_Terms;
end Bar;


   Can anyone tell me what this cryptic error message means and why this is
not compiling?  Please respond by e-mail.


Many thanks in advance,
--
    ____________________________________________________________________
     Chris O'Regan <chris@ECE.Concordia.CA>, UNIX Systems Administrator
              Department of Electrical and Computer Engineering
                   Concordia University, Montreal, Canada
                http://www.ECE.Concordia.CA/~chris/addr.html




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

* Re: Using a procedure as a parameter (Gnat)
  1996-03-26  0:00 Using a procedure as a parameter (Gnat) Chris O'Regan
@ 1996-03-27  0:00 ` Robert A Duff
  0 siblings, 0 replies; 2+ messages in thread
From: Robert A Duff @ 1996-03-27  0:00 UTC (permalink / raw)


In article <4ja0da$442@newsflash.concordia.ca>,
Chris O'Regan <chris@ECE.Concordia.CA> wrote:
>ack.adb:11:35: subprogram must not be deeper than access type

Deeper means more deeply nested within subprograms (packages don't
count).  In your case, you can easily move the procedure into a package,
since it doesn't reference any local variables in the next-outer
procedure.  So put P in a library package, and all will be well.

Sometimes, the procedure you want to pass wants to reference variables
in the next-outer procedure.  In that case, you can use GNAT's
'Unrestricted_Access.  But beware: (1) it's not a standard feature, and
(2) it can cause dangling pointers if you're not careful.  A better
alternative might be to use a generic formal subprogram.

- Bob




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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-03-26  0:00 Using a procedure as a parameter (Gnat) Chris O'Regan
1996-03-27  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