J Cooper
J Cooper

Reputation: 17062

Ada: Renaming an Attribute with a Function?

In a recent question I asked about the ' operation, and learned it is used for getting at language-defined "attributes" of certain types. From what I can gather, there is no way to create your own attributes for your types.

I came across this line of code that I don't understand:

function Image(C: Ada.Containers.Count_Type) return String renames
          Ada.Containers.Count_Type'Image;

What is this doing?

Upvotes: 2

Views: 678

Answers (1)

egilhh
egilhh

Reputation: 6430

Certain attributes, like 'Read, 'Write, 'Input and 'Output, can be overridden by user-defined subprograms, like so:

procedure My_Write
   (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
    Item   : in  My_Type);
for My_Type'Write Use My_Write;

The 'Image attribute can't be overridden. The function definition in you example is a renaming of the attribute, allowing you to call the attribute as if it were a normal subprogram:

Image(My_Count);

instead of

Ada.Containers.Count_Type'Image(My_Count);

Upvotes: 6

Related Questions