'Image attribute for any type¶
Note
Attribute 'Image for any type is supported by
GNAT Community Edition 2020 and latter
GCC 11
'Image attribute for a value¶
Since the publication of the Technical Corrigendum 1 in February
2016, the 'Image attribute can now be applied to a value. So
instead of My_Type'Image (Value), you can just write
Value'Image, as long as the Value is a name. These two
statements are equivalent:
Ada.Text_IO.Put_Line (Ada.Text_IO.Page_Length'Image);
Ada.Text_IO.Put_Line
(Ada.Text_IO.Count'Image (Ada.Text_IO.Page_Length));
'Image attribute for any type¶
In Ada 2022, you can apply the 'Image attribute to any type,
including records, arrays, access types, and private types. Let's see how
this works. We'll define array, record, and access types and corresponding
objects and then convert these objects to strings and print them:
with Ada.Text_IO;
procedure Main is
type Vector is array (Positive range <>) of Integer;
V1 : aliased Vector := [1, 2, 3];
type Text_Position is record
Line, Column : Positive;
end record;
Pos : constant Text_Position := (Line => 10, Column => 3);
type Vector_Access is access all Vector;
V1_Ptr : constant Vector_Access := V1'Access;
begin
Ada.Text_IO.Put_Line (V1'Image);
Ada.Text_IO.Put_Line (Pos'Image);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line (V1_Ptr'Image);
end Main;
$ gprbuild -q -P main.gpr
Build completed successfully.
$ ./main
[ 1, 2, 3]
(LINE => 10,
COLUMN => 3)
(access 7fff64b23988)
Note the square brackets in the array image output. In Ada 2022, array aggregates could be written this way!