Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About "range" in Ada

Tags:

ada

The following source code line in Ada,

type Airplane_ID is range 1..10;

, can be written as

type Airplane_ID is range 1..x;

, where x is a variable? I ask this because I want to know if the value of x can be modified, for example through text input. Thanks in advance.

like image 411
J. C. M. H. Avatar asked Dec 10 '11 02:12

J. C. M. H.


3 Answers

No, the bounds of the range both have to be static expressions.

But you can declare a subtype with dynamic bounds:

X: Integer := some_value;
subtype Dynamic_Subtype is Integer range 1 .. X;
like image 104
Keith Thompson Avatar answered Oct 26 '22 00:10

Keith Thompson


Can type Airplane_ID is range 1..x; be written where x is a variable? I ask this because I want to know if the value of x can be modified, for example through text input.

I assume that you mean such that altering the value of x alters the range itself in a dynamic-sort of style; if so then strictly speaking, no... but that's not quite the whole answer.

You can do something like this:

Procedure Test( X: In Positive; Sum: Out Natural ) is
  subtype Test_type is Natural Range 1..X;
  Result : Natural:= Natural'First;
 begin
   For Index in Test_type'range loop
     Result:= Result + Index;
   end loop;

   Sum:= Result;
 end Test;
like image 35
Shark8 Avatar answered Oct 26 '22 00:10

Shark8


No. An Ada range declaration must be constant.

like image 38
Mitch Wheat Avatar answered Oct 25 '22 23:10

Mitch Wheat