如何(可能?)我基于" new"导出Ada类型?从通用类型?

时间:2015-03-02 04:40:16

标签: generics declaration ada

我很确定这是 new Ada人的陷阱。我觉得有必要做一些简单的事情来做到这一点。我有一些旧代码,带有 Send_Command 过程的声明,带有基于Ada.Strings.Bounded通用模块的 Command_String_Type 类型参数。

 -- -- -- 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过程的参数类型。

当我尝试在模块规范(。 ads )文件中声明Command_String_Type时,我遇到了麻烦。我无法直接获得语法' export'此程序的此类型规范。

 -- -- -- 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

  1. 起初我做了一个直接粘贴的包Command_String_Type是新的Ada.Strings ...构造 - 并收到错误。
    • 这是上面的代码" 目标"标题。
  2. 我认为.ADS文件中的类型声明可以正常工作,因为错误抱怨"没有类型" - 我又一次有错误。
  3. 然后我使用Subtype也遇到了错误。
  4. (更新...)声明 Command_String_Type 私有 - 仍然存在编译错误
  5. 我最后的努力是创建一个Command_String_TypeX的Command_String_Type子类型,并认为我可以在声明中公开子类型。
  6. 到目前为止,我没有五个人。我一直在看书,看着培训笔记,我很失望,我找不到一些出口'出口的例子。来自有界字符串的字符串类型 - 当然,我还在寻找,但是我开始怀疑我的想法是否与其他语言一样简单。这是可能的,并允许与Ada。 感恩提前为您的建议。

1 个答案:

答案 0 :(得分:3)

你提出的规范看起来不错:

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;