In reference to John Barnes' book "Programming in Ada 2012" (ISBN: 978-1-107-42481-4), P. 138 (section 8.6).
procedure ArrayOps is
type Bit_Row is array (Positive range <>) of Boolean;
A, B : Bit_Row (1 .. 4);
T : constant Boolean := True;
F : constant Boolean := False;
begin
A := (T, T, F, F);
B := (T, F, T, F);
A := A and B;
end ArrayOps;
I've added a simple wrapper around the minimal fragment the author provides, and this seems to compile and run as expected.
The author states that this can be done with "Many of the operators", implying arithmetical such as +, *, - and /.
I have attempted to convert this to the addition operator using Integer data types, but alas this does not compile...
procedure ArrayOps is
type Int_Row is array (Positive range <>) of Integer;
A, B : Int_Row (1 .. 4);
T : constant Integer := 1;
F : constant Integer := 0;
begin
A := (T, T, F, F);
B := (T, F, T, F);
A := A + B;
end ArrayOps;
The compiler error states: arrayops.adb:12:10: there is no applicable operator "+" for type "Int_Row" defined at line 2. That is the only error. Clearly something is missing from my code, but the book doesn't mention this topic ever again. How may I enable array operations for operators other than the boolean operators?
EDIT:
I have amended the code as per @egilhh's answer, as that seems to be the minimal set of changes to solve the basic problem.
procedure ArrayOps is
type Int_Row is array (1 .. 4) of Integer;
function "+"(Left, Right : Int_Row) return Int_Row
is
Result : Int_Row;
begin
for I in Int_Row'Range loop --'
Result(I) := Left(I) + Right(I);
end loop;
return Result;
end "+";
A, B : Int_Row;
T : constant Integer := 1;
F : constant Integer := 0;
begin
A := (T, T, F, F);
B := (T, F, T, F);
A := A + B;
end ArrayOps;
This now works. However, I have accepted DeeDee's answer as that may be a best practice solution. Kudos to both :)