[fpc-devel] Forin feature with tBits

Dariusz Mazur darekm at emadar.com
Sat Nov 7 11:32:50 CET 2009


Alexander Klenin pisze:
> 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
>>     
>
> [skipped]
>
>   
>> 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.
>   
I'm not sure, but if this solution is not multithread aware too?
> 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
> implementation.
> 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.
>   
I've thinking about second solution. Now enumerator is passed  through 
operator and its better, when always is the same.
For me it should be done in sysutils, similar as Exception

-- 
  Darek







More information about the fpc-devel mailing list