将类型转换为字符串

A.P*_*cat 3 types ada

我创建了一个类型:

packages MyTypes.Type_A is
    subtype Counter_Type is Integer range 0 .. 15;
    type Array_Counter_Type is record
        Counter_01 : Counter_Type ;
        Counter_02 : Counter_Type ;
        Counter_03 : Counter_Type ;
        Counter_04 : Counter_Type ;
    end record;
end MyTypes.Type_A;
Run Code Online (Sandbox Code Playgroud)

我想这样显示我的数组

MyArray : Array_Counter_Type;
print ( MyTypes.Type_A.Array_Counter_Type'Image (MyArray));
Run Code Online (Sandbox Code Playgroud)

但是我有错误:

前缀og“图片”属性必须为标量类型

我能怎么做 ?是否可以“定制” Image以连接用“-”分隔的4个计数器?

Dee*_*Dee 5

这还不可能,但是它将在Ada 202x [ AI12-0020-1 ]中。在此之前,您将必须定义一个子程序(例如Image)并显式调用它。另请参见此相关问题

使用示例GNAT.Formatted_String

main.adb

with Ada.Text_IO;
with GNAT.Formatted_String;

procedure Main is

   subtype Counter_Type is Integer range 0 .. 15;

   type Array_Counter_Type is
      record
         Counter_01 : Counter_Type;
         Counter_02 : Counter_Type;
         Counter_03 : Counter_Type;
         Counter_04 : Counter_Type;
      end record;

   -----------
   -- Image --
   -----------

   function Image (Array_Counter : Array_Counter_Type) return String is      
      use GNAT.Formatted_String;      
      Format : constant Formatted_String := +"%02d-%02d-%02d-%02d";
   begin      
      return
        -(Format
          & Array_Counter.Counter_01
          & Array_Counter.Counter_02
          & Array_Counter.Counter_03
          & Array_Counter.Counter_04);
   end Image;     

   AC : Array_Counter_Type := (0, 5, 10, 15);

begin
   Ada.Text_IO.Put_Line (Image (AC));
end Main;
Run Code Online (Sandbox Code Playgroud)

但是,如果不可用Ada.Integer_Text_IO,也可以使用,Counter_Type'Image等等GNAT.Formatted_String