AFAIK this is the only way to do such a thing:
That is correct. Although I filed a request to improve this. Currently for some reason the loop runs over the
range (low() to High() including the un-named values, not the actual named enum values.
You corrected this with
continue, but there is no reason from a language point of view to improve it such that only the named items are looped.
The current status is bug prone, see the IOResult 107. Also the
in operator does not work properly because of that. But then again Delphi has the same error-prone behavior.
As per your example, this demonstrates the issue:
program project1;
{$Mode objfpc}{$H+}
type
TDisjointed = (dj1=3, dj2=7, dj3=12, dj4=97);
var
dj: TDisjointed;
s: String;
i:integer = 0;
begin
{$IOChecks OFF}
for dj:= Low(TDisjointed) to High(TDisjointed) do
begin
WriteStr(s, dj);
inc(i);
if IOResult = 107 then
Continue;
WriteLn(s, '=', Ord(dj),' loop count:',i-1);
end;
ReadLn;
end.
As it stands that are a lot of wasted cycles. Better to map them to an array.