[fpc-devel] Re: enumerators

Sven Barth pascaldragon at googlemail.com
Sat Nov 13 22:32:08 CET 2010


On 13.11.2010 20:56, Hans-Peter Diettrich wrote:
> In general, what's the benefit of using enumerators? IMO a for loop
> executes faster on (linear) string and array types, where enumerator
> calls occur in for-in (see also my note on the UTF-8 string example).

I'd say they simplify the code. They might not make the code faster (or 
even make it slower), but they make it easier to read/understand.

Consider the following:

without for-in:

procedure DoSomethingWithSet(aSet: TSetOfEnum);
var
   e: TEnum;
begin
   for e := Low(TEnum) to High(TEnum) do
     if e in aSet then
       // do something
end;

with for-in:

procedure DoSomethingWithSet(aSet: TSetOfEnum);
var
   e: TEnum;
begin
   for e in aSet do
     // do something
end;

The second one is a bit easier to understand.

Basically for-in is syntactic sugar (like "case of string"), nothing 
more, nothing less.

Regards,
Sven



More information about the fpc-devel mailing list