[Lazarus] Easiest way to "case" strings

Alexander Klenin klenin at gmail.com
Thu Mar 26 16:10:40 CET 2009


On Fri, Mar 27, 2009 at 00:22, Paul Ishenin <webpirat at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: iter.pp
Type: application/octet-stream
Size: 1507 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20090327/5c8b4010/attachment-0007.obj>


More information about the Lazarus mailing list