Enumerating enum types in Delphi

One would have thought that enumerating enum types in Delphi would be a no brainer, after all they have it in the name already.

But this does not compile:

var
  L: TSomeEnumType;
begin
  for L in TSomeEnumType do begin
     doSomethingWith(L);
  end;
end;

So I though I’d just wrap it into a record and add an enumerator to that record, especially since I wanted to add some methods to that record anyway, but that didn’t work either. And then it hit me: We have got record helpers and sets can be enumerated, so why not combine both? (Stefan Glienkes Answer also helped.)

type
  TSomeEnumType = (seOne, seTwo, seThree);

type
  TSomeEnumTypeHelper = record helper for TSomeEnumType
  const
    All = [Low(TSomeEnumType) .. High(TSomeEnumType)]
    function Number: integer; // returns Ord(Self) + 1
  end;

var
  L: TSomeEnumType;
begin
  for L in TSomeEnumType.All do begin
     doSomethingWith(L);
  end;
end;

And yes, it works. And it is much cleaner than the record wrapper idea I pursued originally. I wonder if an AI would have come up with that? So I asked one.