What type has a variable containing a range?

A for the loop can be done in Ada using a range with a start and end point:

for I in 0..10 loop
(...)
end loop;

I know that it is possible to execute a for loop using two variables that describe the range:

for I in range_start..range_end loop
(...)
end loop;

Is it also possible to save the range in one variable ?, for example:

for I in my_range loop
(...)
end loop;

What type does the variable * my_range * have?

: , : , , . , , , . , , , , .

+5
5

, :

for I in Arr_Var'Range loop
   ...
end loop;

, , , , .., , ( Ada 2012):

for Elem of Container loop
   ...
end loop;
+3

range, - :

type Range_Type is range -5 .. 10;
...
for A in Range_Type loop

. .

+2

. , (, ) . , "" .

, Ada. -, , 2012 .

+1

If you want to pass an object containing a range, you can transfer the beginning and end to the record. Inside a routine, you can locally declare a new type [sub], limited to a range.

type Range_Type is range TheRange.Start .. TheRange.end;
+1
source

My account says that you need 4 more lines to create a common routine than a simple one:

generic                             -- extra
   type Bounds is (<>);             -- extra
procedure R;

procedure R is
begin
   for J in Bounds'Range loop
      null;
   end loop;
end R;

with R;
procedure P is
   type Rng is range 1 .. 10;       -- extra
   procedure A is new R (Rng);      -- extra
begin
   A;
end P;

which doesn't look so bad.

But as a rule, an iteration iterates over something (an array, ...): if so, you can get a range of iterations from the thing that is renamed.

+1
source

All Articles