[fpc-pascal] High() and Low() for empty dynamic arrays

Jürgen Hestermann juergen.hestermann at gmx.de
Sat Feb 8 16:56:28 CET 2014


Some time ago there was a discussion about the data type
given back by the "length" function.
Several reasons were given for the type beeing a *signed* integer.

But what about High() and Low()?
Shouldn't they give back signed integers too for the same reasons?

Especially, when I have a dynamic array like

--------------------------------
var MyArray : array of ...
...
for i := Low(MyArray) to High(MyArray) do
    MyArray[i] := ...
--------------------------------

I now have to prepend this with a

--------------------------------
if Length(MyArray)<>0 then..
--------------------------------

because otherwise I would get an error in case the length is 0
because Low() and High() both give back 0.
The result of both functions is the same as if the array had exact one element.

I find this quite illogical and would prefer if High() gives
back a value less than Low() in this case so that the above
for-loop would not be entered at all without the need to prepend
it with an if-condition.
For example, setting High() to -1 while Low() to 0 for empty
dynamic arrays would avoid this special case.




More information about the fpc-pascal mailing list