Ada95 子单元是否可以用于分离代码以实现可维护性?

Can Ada95 child subunits be used to separate code for maintainability?

我创建了一个 Ada class,它的实现变得相当庞大。我有一个仅限多个主体的方法,我想出于 maintainability/readability 目的将其移至单独的文件。

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

编辑:上述问题太模糊,任何答案都是主观的。

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

事实上,您可以有 separate 程序包、受保护和任务类型以及子程序的主体。

所以你可以说

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;

并检查

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

同样,您可以将 Implementation 作为子包:

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;

我能看到的唯一区别是 Large 的其他子包可以在子包版本中看到 Implementation 但在单独版本中看不到。

Program Structure section of the Ada Style Guide (2005 edition) 非常喜欢使用子包而不是子单元,并说(在回答您的风格问题时)

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

但是,如您所料,对此会有不同意见。您可以阅读指南的 基本原理 部分,看看它如何适合您的特定情况。

关于 "correctness" 依赖于子包的主体(不是规范,你会得到循环依赖),我一直在使用它,我发现它非常方便,尤其是实现我的包内部使用的一些复杂数据 structure/service。 Ada Style guide (2005) 表示

同意我的看法

Use child packages to implement a subsystem

我想这也是你的情况。