Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2015-10

Iterator for array properties

8 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ondrej Pokorny

When we are already discussing new language features, what about supporting iterators for array properties:

   TTest = class
     // ...
     property Objects[Index: Integer]: TObject read GetObject *iterator
GetObjectIterator*;
     property ObjectCount: Integer read GetObjectCount;
   end;

This would allow code like:

   for MyObject in Test.Objects do
     //...

In the LCL, e.g.:

procedure TForm1.Button1Click(Sender: TObject);
var
   Comp: TComponent;
begin
   for Comp in Components do
     //...
end;

This should actually be pretty OK and should not cause any regressions.

Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote 26 lines
When we are already discussing new language features, what about supporting iterators for array properties: TTest = class // ... property Objects[Index: Integer]: TObject read GetObject *iterator GetObjectIterator*; property ObjectCount: Integer read GetObjectCount; end; This would allow code like: for MyObject in Test.Objects do //... In the LCL, e.g.: procedure TForm1.Button1Click(Sender: TObject); var Comp: TComponent; begin for Comp in Components do //... end; This should actually be pretty OK and should not cause any regressions.

After a

sed s/iterator/enumerator/g

I'm all for it.

I've been looking how to implement names/objects/values enumerators in TStrings. This would be the almost perfect solution.

But your example is not well-chosen, since the enumerator for TComponent already exists:

Procedure TForm1.Button1Click(Sender: TObject);
  var
   Comp: TComponent;
  begin
   for Comp in Self do
     //...
  end;

Just as an aside...

Michael.

Reply by email

Ondrej Pokorny
to Michael Van Canneyt
Michael Van Canneyt wrote 21 lines
After a sed s/iterator/enumerator/g I'm all for it. I've been looking how to implement names/objects/values enumerators in TStrings. This would be the almost perfect solution. But your example is not well-chosen, since the enumerator for TComponent already exists: Procedure TForm1.Button1Click(Sender: TObject); var Comp: TComponent; begin for Comp in Self do //... end; Just as an aside...

Yes, of course. I ment enumerator :)

   TTest = class
     // ...
     property Objects[Index: Integer]: TObject read GetObject
*enumerator GetObjectEnumerator*;
     property ObjectCount: Integer read GetObjectCount;
   end;

+ Yes, the "in Self" is supported. But this is not the problem here. The example still doesn't compile. A class can have more array properties. And even the default class enumerators can be redeclared and so hide enumerators from ancestor classes.

Maybe a better example:

*Procedure TForm1.Button1Click(Sender: TObject); ** ** var **
** Cont: TControl; **
** begin **
** for Cont in Controls do **
** //... **
** end; **
***
Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote 22 lines
On 10.10.2015 12:30, Michael Van Canneyt wrote: > After a > > sed s/iterator/enumerator/g > > I'm all for it. + Yes, the "in Self" is supported. But this is not the problem here. The example still doesn't compile. A class can have more array properties. And even the default class enumerators can be redeclared and so hide enumerators from ancestor classes. Maybe a better example: *Procedure TForm1.Button1Click(Sender: TObject); ** ** var ** ** Cont: TControl; ** ** begin ** ** for Cont in Controls do ** ** //... ** ** end; ** ***

100% agreed :)

Michael.

Reply by email

Ondrej Pokorny
to Ondrej Pokorny

Oh, it would be so nice if default enumerator wasn't defined with the "GetEnumerator" function but with the default property:

   TTest = class
     // ...
     function GetObjectEnumerator: TTestObjectEnumerator;
     property Objects[Index: Integer]: TObject read GetObject enumerator
GetObjectEnumerator; default;
   end;

In this case the default enumerator would always correspond with the default property. Now they can be completely different. But Embarcadero already made the decision, so it doesn't matter :(

Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote 13 lines
Oh, it would be so nice if default enumerator wasn't defined with the "GetEnumerator" function but with the default property: TTest = class // ... function GetObjectEnumerator: TTestObjectEnumerator; property Objects[Index: Integer]: TObject read GetObject enumerator GetObjectEnumerator; default; end; In this case the default enumerator would always correspond with the default property. Now they can be completely different. But Embarcadero already made the decision, so it doesn't matter :(

The class does not need to have a default property to begin with, so it is somewhere logical.

Michael.

Reply by email

Ondrej Pokorny
to Michael Van Canneyt
Michael Van Canneyt wrote
The class does not need to have a default property to begin with, so it is somewhere logical.

Yes, in that case a default enumerator would not be allowed, which sounds (in contrary) logical to me: no default array property => no enumerator. But this depends on the point of view, I can understand that the meanings differ. And it is a pure off-topic because it has already been decided :)

Ondrej

Reply by email