From: chris@ECE.Concordia.CA (Chris O'Regan)
Subject: Using a procedure as a parameter (Gnat)
Date: 1996/03/26
Date: 1996-03-26T00:00:00+00:00 [thread overview]
Message-ID: <4ja0da$442@newsflash.concordia.ca> (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
next reply other threads:[~1996-03-26 0:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-03-26 0:00 Chris O'Regan [this message]
1996-03-27 0:00 ` Using a procedure as a parameter (Gnat) Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox