On Fri, Mar 27, 2009 at 00:22, Paul Ishenin <webpi...@mail.ru> wrote:
>
> FOR s IN list DO
>  WriteLn(s);
>
> extended:
>
> FOR s IN list USING my_reverse_iterator DO
>  WriteLn(s);
>
> In upper case I wrote keywords and in lower case identifiers.

I think the extension can be avoided like so:

FOR s IN reverse(list) DO

where reverse is a standard (or user-defined) function returning iterator.

FOR s IN list DO

will be a syntax sugar for

FOR s IN default_iterator(list) DO

where default_iterator is defined for built-in containers and can be
redefined for
user-defined ones.

This can _almost_ be emulated at the library level -- look at the
attached example.

However, few problems exist:
1) overloading of operator := was designed without thinking, it should
be a procedure
  with 'var' parameter instead of a function, because assignment often
requires access
  to a previous state of l-value. So, rather ugly 'property V' is required.
2) 'while ForEach(a, r) do' is IMO less clear syntax than 'for a in r do'
3) user have to declare 'a: TIntegerArrayIterator' instead of simply
'a: Integer'
4) Note that I overloaded operator + only.
  In practice, all relevant operators should be overloaded, which is bulky.
5) Finally, all this code have to be written for every
container/element combination.
  Generics might or might not help here -- but at this stage built-in
implementation
  would actually be simpler then library-based one.
-- 
Alexander S. Klenin

Attachment: iter.pp
Description: Binary data

_______________________________________________
Lazarus mailing list
Lazarus@lazarus.freepascal.org
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to