首页 文章

如何(可能?)我根据通用的“新”类型导出Ada类型?

提问于
浏览
0

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

old

-- -- -- 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时,我遇到了麻烦 . 我可以't get the syntax straight to '导出'此程序的此类型规范 .

target

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

  • 起初我做了一个直接粘贴的包Command_String_Type是新的Ada.Strings ...构造 - 并收到错误 .

  • 这是“目标” Headers 下面的代码 .

  • 我认为.ADS文件中的类型声明会起作用,因为错误抱怨"no type" - 我又有错误 .

  • 然后我使用Subtype也出错了 .

  • (更新...)声明Command_String_Type为私有 - 仍然存在编译错误

  • 我最后的努力是创建一个Command_String_TypeX的Command_String_Type子类型,其思想是我可以在声明中公开子类型 .

到目前为止,我已经阅读了书籍并查看了培训笔记,我发现了一些例子'exports'来自有界字符串的字符串类型 - 我开始怀疑我认为是否像其他人一样直截了当语言 . 这是可能的,并允许与Ada . 提前感谢您的建议 .

1 回答

  • 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_Interface 的可见部分创建了包 Command_String_Type ,因此您可以将其引用为 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;
    

相关问题