[fpc-devel] New feature discussion: for-in loop
Paul Ishenin
webpirat at mail.ru
Sat Oct 24 22:01:37 CEST 2009
Paul Ishenin wrote:
> The next feature which we should think of is for-in loop:
> http://wiki.lazarus.freepascal.org/for-in_loop
At the moment implementation is almost complete and it is possible to
test and review it (although many checks still needs to be added). It
unites 2 approaches: delphi - search enumerators by symbol names and fpc
- search using operators and method/property modifiers.
Enumerator can be found for type using:
1. GetEnumerator method to the class/object (delphi way). For example:
type
TSomeClass = class
public
function GetEnumerator: TSomeEnumerator;
end;
2. operator enumrator for any type (fpc extension). For example:
operator enumerator (const s: string): TStringEnumerator;
begin
Result := TStringEnumerator.Create(s);
end;
Enumerator is a class or object which need to have:
1. function MoveNext: Boolean; and property Current: sometype; (delphi
way) For example:
TStringEnumerator = class
private
function GetCurrent: Char; inline;
public
function MoveNext: Boolean;
property Current: Char read GetCurrent;
end;
2. function with any name and Boolean return type marked by 'enumerator
MoveNext' directive and a property with any name marked by 'enumerator
Current' directive (fpc extension). For example:
TStringEnumerator = class
private
function GetValue: Char; inline;
public
function StepForward: Boolean; enumerator MoveNext;
property Value: Char read GetValue; enumerator Current;
end;
Enumerator search for the paticular type performs in the next order:
1) search available operator
2) if it is a object/class instance then search for GetEnumerator method
3) if it is a enumeration/range/array/string/set then use built-in
enumerator support for this type
A branch to test is:
http://svn.freepascal.org/svn/fpc/branches/paul/features
Enumerator tests are: tests\test\tforin1.pp ... tests\test\tforin7.pp
A brief explanation of how it works can be found here:
http://wiki.lazarus.freepascal.org/for-in_loop#Proposed_implementation
Suggestions and comments are welcome except the new discussion about
enumerators necessity.
Best regards,
Paul Ishenin.
More information about the fpc-devel
mailing list