[fpc-devel] Forin feature with tBits

Alexander Klenin klenin at gmail.com
Sat Nov 7 00:12:54 CET 2009

2009/11/7 Dariusz Mazur <darekm at emadar.com>:
> If try to apply forin feature to tBits.
> But I have two problems
> 1. function FindFirstBit has bug


> 2. findfirstbit is not multithread aware (because state is per object, not
> local)

Yes, this is a good example why separate iterators are preferable to
the ones built into container.

> Should I prepare patch?
> Whats about used names?

IMHO your names (EnumSet/EnumClear) are ok.
Obviously, the implementation should not call FindFirstBit,
but copy it's logic. To avoid code duplication,

I think you can to it the other way around -- let FindFirstBit
create a enumerator and store it inside the TBits instance.
It would be even better to drop FindFirstBit/FindNextBit altogether,
but I do not know how large compatibility problem it might cause.

[skipped most of code]

> function tEnumBits.enumSet:tBitEnumerator;
> begin
>  result:=tBitEnumerator.create(self,true);
> end;
> function tEnumBits.enumClear:tBitEnumerator;
> begin
>  result:=tBitEnumerator.create(self,false);
> end;

> operator enumerator (AEnumerator: TBitEnumerator): TBitEnumerator;
> begin
>  Result := AEnumerator
> end;

IMHO, this code demonstrates an inconvenience of the current for..in
The last "operator enumerator" declaration is clearly redundant.
I have two alternative suggestions here:
1) Let for..in accept the enumerator instead of the container as the
second argument.
2) Define the trivial "operator enumerator" as above for some basic
type, e.g. TEnumerator,
maybe in Classes.pp or even System unit. After that, one can inherit
TBitEnumerator from TEnumerator to avoid the need for "operator
enumerator" declaration.

Paul Ishenin, what do you think?

Alexander S. Klenin

More information about the fpc-devel mailing list