[英]How (may?) I export Ada type based on a “new” type from a generic?
我很确定这是Ada 新手的陷阱。 我觉得有一些简单的方法可以解决这个问题。 我有一些较旧的代码,带有基于Ada.Strings.Bounded通用模块的带有Command_String_Type类型参数的Send_Command过程的声明。
旧
-- -- -- command.ads -- -- --
-- nothing relevant, Send_command is/was internal to module.
:
-- -- -- command.adb -- -- --
-- Type to hold command to send while it is being constructed.
package Command_String_Type is
new Ada.Strings.Bounded.Generic_Bounded_Length(Max => Command_Max);
:
:
procedure Send_Command(
Command_String : in Command_String_Type.Bounded_String );
这: Command_String_Type用作此模块必须从该模块导出的Send_Command过程的参数类型。
我的麻烦来当我尝试申报模块规范( 广告 )文件Command_String_Type。 对于该过程,我无法直接将语法“导出”此类型规范。
目标
-- -- -- command_interface.ads -- -- --
package Command_Interface is
Command_Max : constant := 200;
:
-- WANTED ... export Command_String_Type from this module
-- Type to hold command to send while it is being constructed.
package Command_String_Type is
new Ada.Strings.Bounded.Generic_Bounded_Length(Max => Command_Max);
:
:
procedure Send_Command(
Command_String : in Command_String_Type.Bounded_String );
:
end Command_Interface; -- specification
-- -- -- command_interface.adb -- -- --
package body Command_Interface is
:
procedure Send_Command(
Command_String : in Command_String_Type.Bounded_String )
is
begin
:
-- implementation ...
:
end Send_Command;
end Command_Interface; --package
很自然,Ada-95编译器需要一个“ Command_String_Type ”,如上所示。 想要将Send_Command过程放在command_interface包中。 并将其导出以供其他模块使用。 此过程取决于Command_String_Type
到目前为止,我已经五岁了。 我一直在看书并查看培训说明,但很失望,我找不到一些示例,可以从有界字符串中“导出”字符串类型-当然,我仍然在寻找,但是我开始想知道我认为的语言是否和其他语言一样简单。 Ada可以并允许这样做吗? 提前感谢您的建议。
您提出的规格看起来不错:
with Ada.Strings.Bounded;
package Command_Interface is
Command_Max : constant := 200;
package Command_String_Type is
new Ada.Strings.Bounded.Generic_Bounded_Length (Max => Command_Max);
procedure Send_Command
(Command_String : in Command_String_Type.Bounded_String);
end Command_Interface;
身体也是如此(当然,这是一个演示):
with Ada.Text_IO;
package body Command_Interface is
procedure Send_Command
(Command_String : in Command_String_Type.Bounded_String) is
begin
Ada.Text_IO.Put_Line (Command_String_Type.To_String (Command_String));
end Send_Command;
end Command_Interface;
现在我们开始使用它。 您所创建的包Command_String_Type
中的可见部分Command_Interface
,所以你可以参考它作为Command_Interface.Command_String_Type
:
with Command_Interface;
procedure Check_Command_Interface is
begin
Command_Interface.Send_Command
(Command_Interface.Command_String_Type.To_Bounded_String ("hello!"));
end Check_Command_Interface;
您可能会觉得这有点冗长。 您可以缩短实例化的名称,例如
package Command is
new Ada.Strings.Bounded.Generic_Bounded_Length (Max => Command_Max);
或者您可以在通话附近使用软件包重命名的技巧,
procedure Check_Command_Interface_2 is
package CICST renames Command_Interface.Command_String_Type;
begin
Command_Interface.Send_Command (CICST.To_Bounded_String ("hello!"));
end Check_Command_Interface_2;
或者您可以落水而use
:
procedure Check_Command_Interface_3 is
use Command_Interface.Command_String_Type;
begin
Command_Interface.Send_Command (To_Bounded_String ("hello!"));
end Check_Command_Interface_3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.