Question

I'm trying to write an Ada package in the following way:

p.spec

package P is

   generic
      type Input;
      procedure A (x: in Input);

   generic 
      procedure B;

   generic
      procedure C;

end P;

p.body

package body P is

   procedure A(x: in Input) is separate;
   procedure B is separate;
   procedure C is separate;

end P;

p.a.body

-------------

p.b.body

-------------

p.c.body

-------------

In the program, procedures B,C are calling procedure A. During compilation, there is an error because when procedures B,C are compiled, procedure A has not yet been compiled. Any suggestions?

Was it helpful?

Solution

When B tries to call A, how is it going to know which A? What is the type Input to be?

Usually, you’d solve this by including all of A, B and C in a generic package;

package P is
   generic
      type Input is private;
   package G is
      procedure A (X : Input);
      procedure B;
      procedure C;
   end G;
end P;

If you need the structure you’ve shown, you have to write

generic
   type Actual_Input is private;
   with procedure Actual_A (X : Actual_Input);
procedure B;

and then instantiate by

procedure My_A is new P.A (Input => Integer);
procedure My_B is new P.B (Actual_Input => Integer, Actual_A => My_A);

In a different language, formal subprograms would be like formal packages and you could write something like

generic
   type Actual_Input is private;
   with procedure Actual_A (X : Actual_Input) is new A (Input => Actual_Input);
procedure B;

to make sure that the Actual_A was in fact an instance of A. But that’s not Ada.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top