[fpc-devel] Limitations of static "ranged" integer types andsucc/pred, and dynamic solution.

Skybuck Flying skybuck2000 at hotmail.com
Fri Dec 23 03:39:03 CET 2011


The reason for the proposal to add a dynamic/runtime version of 
Low/High/Succ/Pred is pretty simply:

Currently there simply is no dynamic/runtime version of it.

Thus any code which is written using low/high/succ/pred is static in nature, 
when the code needs to be changed to dynamic it's likely that this will 
cause problems.

The problems are a bit vague, perhaps even hard to spot, correct and 
understand, I am not going to examine these problems in detail right now, 
but these problems probably exist.

All that is needed to fix these problems is a dynamic version of these 
methods. But then again this might require even more work to have dynamic 
integer types/ranges as well to complement/support it.

I'll shall try to give one example of a potential problem:

Situation 1:

type
  Tvalue = 0..5000;

var
  vIndex : TValue;
  vA : TValue;
  vB : TValue;

for Index := Low(TValue) to High(TValue) do
begin
   if vA = High(TValue) then
   begin
       vA := Low(TValue);
   end else
   begin
       vA := Succ(vA);
   end;
end;

^ This is a real world example and will probably compile just fine or 
perhaps with a few correction.

Now suppose the Tvalue can no longer be static and must be changed to 
dynamic, a choice could be made to turn this into a smallint like so:

TValue = smallint

Suddenly this changes the range of low, high, succ, pred.

At runtime the real range is set, for example to 32000.

This breaks all the code, the wrap backs will be wrong, the vA will go out 
of range and will go to 32768 and perhaps even wrap back to negative.

Typecasts might also add to the problem, not sure about that.

Thus by simply changing the "ranged" static Tvalue to a more "open range" 
static integer type a whole can of worms/problems is opened.

These problems could be more easily solved if Low/High/Succ/Pred was more 
dynamic in nature and could adept to the runtime change.

Bye,
  Skybuck. 




More information about the fpc-devel mailing list