user5069935
user5069935

Reputation:

Can't get simple Ada Iterator example to compile

I'm learning Ada and I am trying to get a very simple loop working (using the new iterators syntax from Ada 2012). I cannot see what's wrong...

with Ada.Text_IO;
use Ada.Text_IO;

procedure Arrays is
  type V is array (0 .. 1, 0 .. 2) of Natural;
  A : V := ((10, 20, 30), (400, 500, 600));
begin
  for E of A loop  --  compiler error here!
    E := E + 1;
  end loop;
end Arrays;

My compile command is "$ gnatmake -gnaty -gnaty2 -gnat12 arrays" (for pedantic style enforcement and to enable 2012 features). The compiler error is

arrays.adb:8:14: too few subscripts in array reference

(I'm using gnatmake 4.6 on a Raspi).

This code is pieced together from John Barnes' book "Programming in Ada 2012" p.120-121. I've trimmed down this code as much as I can, that's why it doesn't do much. As far as I can see, it's effetively identical to the examples in the book.

What am I doing wrong?

Upvotes: 0

Views: 514

Answers (1)

Wayne
Wayne

Reputation: 78

Use a nested array.

    procedure Iterator is
        type rows is array (0 .. 2) of Natural;
        type columns is array (0 .. 1) of rows;
        A : columns := ((10, 20, 30), (400, 500, 600));
    begin
        for column of A loop
            for E of column loop
                E := E + 1;
                put_line (e'img);
            end loop;
        end loop;
    end Iterator;

Upvotes: 2

Related Questions