我有一个不寻常的情况,根本没有执行精简代码。这不是详细说明问题,而是所有问题的详细阐述。
问题在于我没有“讨论”有关的单位,但理论上它仍然应该是可访问的,只要它的详细说明发生。
当然,我可以为相关单元添加一个无用的“with”,但在我的实际用例中,我必须使用大量单元。
我的问题是,如果在代码中,通过编译指示,在gpr项目文件中,或通过命令行开关,我可以强制编译器包含文件,即使它认为文件未被引用?
这是一个最小的工作示例:
阿斯.ADS:
package As is
type A is tagged null record;
type Nothing is null record;
function Create (Ignored : not null access Nothing) return A;
function Image (From : A) return String;
end As;
as.adb:
package body As is
function Create (Ignored : not null access Nothing) return A is
(null record);
function Image (From : A) return String is ("A");
end As;
finder.ADS:
with Ada.Tags;
package Finder is
procedure Register (Name : String; Tag : Ada.Tags.Tag);
function Find (Name : String; Default : Ada.Tags.Tag) return Ada.Tags.Tag;
end Finder;
finder.adb:
with Ada.Containers.Indefinite_Vectors;
package body Finder is
type Name_Tag (Size : Natural) is
record
Name : String (1 .. Size);
To : Ada.Tags.Tag;
end record;
package Name_Tag_Vectors is new Ada.Containers.Indefinite_Vectors (Positive, Name_Tag);
Name_Tags : Name_Tag_Vectors.Vector := Name_Tag_Vectors.Empty_Vector;
procedure Register (Name : String; Tag : Ada.Tags.Tag) is begin
Name_Tags.Append ((Name'Length, Name, Tag));
end Register;
function Find (Name : String; Default : Ada.Tags.Tag) return Ada.Tags.Tag is begin
for Tag of Name_Tags loop
if Tag.Name = Name then
return Tag.To;
end if;
end loop;
return Default;
end Find;
end Finder;
不是.ADS:
with As;
package Bs is
type B is new As.A with null record;
function Create (Ignored : not null access As.Nothing) return B;
function Image (From : B) return String;
end Bs;
bs.adb:
with Finder;
package body Bs is
function Create (Ignored : not null access As.Nothing) return B is
(As.Create (Ignored) with null record);
function Image (From : B) return String is ("B");
begin
Finder.Register ("B", B'Tag);
end Bs;
test.adb:
with As; use As;
-- with Bs; -- (uncommenting this line solves my problem, but what if I had the rest of the alphabet?)
with Finder;
with Ada.Tags.Generic_Dispatching_Constructor;
with Ada.Text_IO;
procedure Test is
function Constructor is new Ada.Tags.Generic_Dispatching_Constructor (
T => A,
Parameters => Nothing,
Constructor => Create);
Nada : aliased Nothing := (null record);
What : A'Class := Constructor (Finder.Find ("B", A'Tag), Nada'Access);
begin
Ada.Text_IO.Put_Line (What.Image);
end Test;
我所做的(例如here ff)实际上是引用主程序中的单位(使用pragma Unreferenced
来防止警告)。
或者,你可以有一个包,例如包括所有必要的Required_Units
s的with
,然后来自主程序的with
。
即使有一些替代过程,你也必须告诉它你需要包含哪些单位;也许顺其自然,在Ada做吧!
编译器认为你的包Bs
没有被引用,因为它不是。你没有with
条款,因此它不属于你的程序。
一个简单的例子:
啊.ADS
package A is
procedure Blah;
end A;
a.adb
with Ada.Text_IO;
package body A is
procedure Blah is begin null; end Blah;
begin
Ada.Text_IO.Put_Line("Elaborate A");
end A;
不.ADS
package B is
procedure Blah;
end B;
b.adb
with Ada.Text_IO;
package body B is
procedure Blah is begin null; end Blah;
begin
Ada.Text_IO.Put_Line("Elaborate B");
end B;
main.adb
with Ada.Text_IO;
with A;
procedure Main is
begin
Ada.Text_IO.Put_Line("Main");
end Main;
当我运行main
时,它会打印出来
Elaborate A
Main
它不打印Elaborate B
,因为该包不是该程序的一部分;它只是同一目录中的几个源文件。
显而易见的解决方案是添加with
条款。
我不知道是否有一个不太明显的解决方案。如果有,它可能是编译器特定的。但我不确定为什么编译器会有一个功能,允许您将一个未使用的包合并到一个程序中。
由于程序包Bs对您的程序是不可见的,因此类型B也是如此。
所以下一个问题是:如果不在任何地方使用,为什么需要注册B类?
如果Ada编译器确实详细说明了与主程序无关但通过源路径可见的所有单元(包或独立子程序),它将变得非常混乱!...