1

I have created an Ada class whose implementation has become quite large. I have a multiple body only methods that I would like move to a separate file for maintainability/readability purposes.

  • My understanding with Ada95 Separates is that you can only have one method per file. I have about 20 methods I want to separate but it is undesirable to me to create 20 separate files for this functions.

  • To separate the code I was thinking I could create a child package. Then the parent body could call the child class.

Q.1 In Ada, Is it wrong/undesirable for a Parent Body unit to depend on a child Unit?

EDIT: The above question is too vague and any answer would be subjective.

Q.2 How can I divide my code into separate files without creating an over abundance of files?

Jerunh
  • 524
  • 5
  • 16

2 Answers2

5

In fact you can have separate bodies for packages, protected and task types, as well as subprograms.

So you could say

package Large is
   procedure A;
   procedure B;
end Large;

package body Large is
   package Implementation is
      procedure A;
      procedure B;
   end Implementation;
   package body Implementation is separate;
   procedure A renames Implementation.A;
   procedure B renames Implementation.B;
end Large;

with Ada.Text_IO; use Ada.Text_IO;
separate (Large)
package body Implementation is
   procedure A is
   begin
      Put_Line ("large.implementation.a");
   end A;
   procedure B is
   begin
      Put_Line ("large.implementation.b");
   end B;
end Implementation;

and to check

with Large;
procedure Check_Separate_Implementation is
begin
   Large.A;
   Large.B;
end Check_Separate_Implementation;

Equally, you could have Implementation as a child package:

private package Large.Implementation is
   procedure A;
   procedure B;
end Large.Implementation;

with Large.Implementation;
package body Large is
   procedure A renames Implementation.A;
   procedure B renames Implementation.B;
end Large;

The only difference I can see is that other child packages of Large would be able to see Implementation in the child package version but not in the separate version.

The Program Structure section of the Ada Style Guide (2005 edition) strongly prefers use of child packages rather than subunits, and says (in answer to your style question)

In preference to nesting in a package body, use a private child and with it to the parent body.

but, as you might expect, opinions will differ on this. You could read the Rationale section of the Guide and see how it fits your particular situation.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • Thank you @Simon Wright! This is exactly the solution that I originally sought. I'm not sure that it answers the original question of correctness to depend on a child unit. I have updated my question to reflect my intent. – Jerunh Jun 15 '16 at 11:36
  • @Jerunh, I added a note about the Ada Style Guide. – Simon Wright Jun 15 '16 at 16:58
-1

About the "correctness" of having a body (not a spec, you would get a circular dependency) depending on a child package, I use that all the time and I find it very convenient, especially to implement some complex data structure/service used internally by my package. The Ada Style guide (2005) agrees with me when it says

Use child packages to implement a subsystem

I guess this is your case too.

Riccardo B.
  • 191
  • 3