Casey Kuball
Casey Kuball

Reputation: 7955

Ada - Any defined behavior for incrementing past end of range?

If I define a range in ADA as being from 1 .. 1000, is there a defined behavior by the ADA spec if I increment past 1000?

For example:

type Index is range 1..1000;

idx : Index := 1;

procedure Increment is 
begin
  idx := idx + 1;
end

What should I expect to happen once I call Increment with idx = 1000?

Upvotes: 2

Views: 266

Answers (1)

morido
morido

Reputation: 1017

Your program will fail with a CONSTRAINT_ERROR. However, this is not because you eventually try to set idx to 1001. Rather is its initial value of 0 not within your predefined range either. Thankfully, the compiler will already warn you at compile time about this fact.

If you had set idx to a permitted value and then incremented it beyond its upper limit in a way the compiler cannot statically detect, again CONSTRAINT_ERROR will be raised (but there won't be any hint at compile time). This error is technically an exception which you can handle like any other exception in that language.

Note: I intentionally linked to the ancient Ada '83 specs above to show that this behavior has been part of the language since the beginning of time.

Upvotes: 5

Related Questions