[Lazarus] const array syntax

Hans-Peter Diettrich DrDiettrich1 at aol.com
Wed Jan 13 22:03:03 CET 2010


Doug Chamberlin schrieb:

>> Must confess, I feel "arr:[0..end] of " makes more sense to me than 
>> use of "var" or "..]" The latter suggests something forgotten or a typo.
> 
> I agree that just leaving out the ending limit is against the spirit of 
> Pascal.
> 
> I can live with the syntax of MyArray:[0..var] or MyArray:[0..end] but 
> I'd prefer a syntax that does not use a word, such as MyArray:[0..*] or 
> MyArray:[0..?]. That way there is no confusion about a user-defined word 
> and the use of the currently reserved words is not changed. In other 
> words, this is a situation that is exactly why we have punctuation and 
> not the cobol-ish "Declare MyArray to be from 0 to end of TSomeType"!

Since we already have an (dynamic) array syntax without any "[...]" 
clause, I'd choose an way in between "array", "array[rangetype]" and 
"array[from..to]", e.g. array[], array[const] or array[*] for arrays 
with automatic bounds. In detail the "const" keyword is used similarly 
in argument lists "array of const".

As already mentioned, I do not favor a [from..?] syntax, because then it 
also should be possible to construct [?..to] arrays, for syntax 
symmetry. When the array bounds are not named or literal constants, a 
For loop will have to look like:
   for i := low(MyArray) to high(MyArray) do
so that it doesn't matter which exact values low() and high() really 
represent in code. That's why IMO a fixed lower bound of zero does no 
harm. Any other loops, with fixed bounds, will either fail at compile 
time or - even worse - produce wrong results, when the bounds are 
changed in the array declaration but not in code.

DoDi





More information about the Lazarus mailing list