I am having trouble getting dynamic dispatching to work with Ada generics. Is there an elegant way (something better than conditions such as if X in A then ...
) to have the generic Print
procedure in package Test_Generics
call the corresponding more specific Print
procedures of the child objects?
with Ada.Text_IO;
procedure Test is
type Object is
abstract tagged limited null record;
type A is
limited new Object with null record;
type B is
limited new Object with null record;
generic
type T is abstract tagged limited private;
with procedure Print (X : access T'Class);
package Test_Generics is
procedure Call_Print (X : access T'Class);
end Test_Generics;
package body Test_Generics is
procedure Call_Print (X : access T'Class) is
begin
X.Print;
end Call_Print;
end Test_Generics;
procedure Print (X : access Object'Class) is
begin
X.Print; -- dispatch procedure call to one of the next two
end Print;
procedure Print (X : access A) is
begin
Ada.Text_IO.Put_Line ("Print A");
end Print;
procedure Print (X : access B) is
begin
Ada.Text_IO.Put_Line ("Print B");
end Print;
package Impl is new Test_Generics (Object, Print);
A1 : access A := new A;
B1 : access B := new B;
begin
Impl.Call_Print (A1);
end Test;
Error message
raised STORAGE_ERROR : test.adb:34 infinite recursion