Inheriting from generic child package

I have a generic parent package A and its child package A.1.
In A a type T is defined.
In A.1 some primitive operations for type T are implemented.
When I instantiate A in package M then the instantiated package M can see T. In package M I derive a new type S from T. But the type S does not inherit the primitive operations from the generic child package A.1.

What can I do to make S inherit the stuff from A.1 ?

What you describe is illegal. You cannot implement primitive operations in another package, only in a separate body. You probably meant something else, so a small code sample might help to understand the problem.

This is what I was afraid of. You did understand me absolutely right. My idea was to clean up the code in package A and separate primitive operations for T in A.1. So I have to keep all in package A. Thanks for your reply.

Separate body is exactly for that. For example:

------------ p.ads -----------------------
package P is
   type T is tagged null record;
   procedure Foo (X : in out T);
end P;
------------ p.adb -----------------------
package body P is
   procedure Foo (X : in out T) is separate;
end P;
------------ p-foo.adb -----------------------
separate (P)
procedure Foo (X : in out T) is
begin
   null;
end Foo;

Defining a type without any primitive operations in some package does not make much sense.
Just move the type declaration T also in A.A1.

What I’ve done in the past is an extension of what Dmitry mentioned. I have had a lot of subprograms that I wanted moved to another file for cleanliness, so what I do is declare a private nested package with all of those subprograms I want, do a renames on them, then implement them in the “separate” package body file. Example below:

generic_base.ads

generic
   type Element_Type is private;
package Generic_Base is

   type Instance is tagged private;
   procedure Primitive_1(Self : Instance); -- This will be local
   procedure Primitive_2(Self : Instance); -- This will be local
   procedure Primitive_3(Self : Instance); -- This will be in a separate file
   procedure Primitive_4(Self : Instance); -- This will be in a separate file

private

   type Instance is tagged null record;

end Generic_Base;

generic_base.adb

package body Generic_Base is

   -- Local implementations
   procedure Primitive_1(Self : Instance) is null;
   procedure Primitive_2(Self : Instance) is null;

   -- All of the procedures I want to put into a separate file.
   -- Pick a better package name obviously, this is just for example
   package Primitives is
      procedure Primitive_3(Self : Instance);
      procedure Primitive_4(Self : Instance);
   end Primitives;
   package body Primitives is separate;

   -- These rename clauses link the seprate package implementations to
   -- to the primivitive declrations in the package spec
   procedure Primitive_3(Self : Instance) renames Primitives.Primitive_3;
   procedure Primitive_4(Self : Instance) renames Primitives.Primitive_4;

end Generic_Base;

generic_base-primitives.adb

separate
(Generic_Base)
package body primitives is
   procedure Primitive_3(Self : Instance) is null;
   procedure Primitive_4(Self : Instance) is null;
end primitives;
1 Like

Interesting. I never knew a primitive operation can be implemented as a simple rename. Thanks for the example.