Question out of curiousity: homograph of & (and operators in general)

While experimenting, I came across the “homograph clash issue”, which brought a question. An concrete example: first snippet works fine, the second give compile error.

with Ada.Containers; use Ada.Containers;
with Ada.Containers.Vectors;
procedure Q_good is
  package Int_Vectors is new Ada.Containers.Vectors (Positive, Integer);
  use Int_Vectors;
    V : Vector := 1 & 11 & 111;
begin
  null;
end Q_good;

However, removing use Int_Vectors and using the dot notation produces compile error (below):

with Ada.Containers; use Ada.Containers;
with Ada.Containers.Vectors;

procedure Q_bad is
    package Int_Vectors is new Ada.Containers.Vectors (Positive, Integer);
    V : Int_Vectors.Vector := 1 & 11 & 111;
begin
  null;
end Q_bad;

gcc -c q_bad.adb q_bad.adb:6:33: error: invalid operands for concatenation q_bad.adb:6:33: error: maybe "and" was meant gnatmake: "q_bad.adb" compilation error

In this example the compiler is interpreting the & as a homograph for and, which explains the need for “use” clause ,

But, this makes me wonder: How would one signify that the ampersand should be associated with the package Int_Vectors ? I tried variations on Int_Vectors.& etc. with no luck.

with Ada.Containers; use Ada.Containers;
with Ada.Containers.Vectors;

procedure Q_bad is
    package Int_Vectors is new Ada.Containers.Vectors (Positive, Integer);
    V : Int_Vectors.Vector := Int_Vectors."&" (Int_Vectors."&" (1, 11), 111);
begin
  null;
end Q_bad;

If you look into the definition of Ada.Containers.Vectors, the “&” operator is defined there. Specifically http://www.ada-auth.org/standards/22rm/html/RM-A-18-2.html Line 15/5 and following

Operator name is quoted when used as a name, i.e. Int_Vectors."&". (The relevant RM part is 4.1 where operator_symbol defined as string_literal.)

Then instead of a full use clause you can try use type Int_Vectors.Vector to make & and other operations on Vector visible.

Thank you for both replies. I had totally forgotten to check the Ada Reference Manual.

Sorry about that. But, my embarrassment will serve to remind me to do this in the future!

Nope: & and and are two different operators.
(Yes, renames can be used [by the type’s author] to make them the same, but this is not necessarily the case.)

Yes, you are right, I was confusing the error message about maybe "and" was meant.