Yes it is more clear. Although I wished there was no need for a hack like this, still better than using a loop!
This is not a hack. The index of a static array is either a range or an ordinal type.
I want to make an array of all possible values of enum and range type, in an easy way, without loops. Something like the sample. Is there a way, as the sample does not work.
program Project1;
type
TEnum = (A, B, C, D);
TRange = 1..10;
var
EnumArray:array of TEnum=(Low(TEnum)..High(TEnum));
RangeArray:array of TRange=(Low(TRange)..High(TRange));
begin
end.
No, there is no way to do this at compile time except you write out all values as
jamie showed. However you can create a generic function that does what you want, this way you only need to write this out only once:
program range;
{$mode objfpc}
type
TEnum = (A, B, C, D);
TRange = 1..10;
generic function GenerateRangeArray<T>: specialize TArray<T>;
var
i: SizeInt;
begin
SetLength(Result, Ord(High(T)) - Ord(Low(T)) + 1);
for i := 0 to High(Result) do
Result[i] := T(Ord(Low(T)) + i);
end;
var
EnumArray:array of TEnum;
RangeArray:array of TRange;
begin
EnumArray := specialize GenerateRangeArray<TEnum>;
RangeArray := specialize GenerateRangeArray<TRange>;
end.