[fpc-pascal] "is"
Marco van de Voort
marcov at stack.nl
Wed Apr 6 08:50:50 CEST 2005
> the main thing.
>
> As far as I understand the proposal the main point for this construct is
> the use in iterable container classes (implementing a certain
> interface). And there it can show it's advantages (or what I think that
> are advantages):
>
> Instead of a very longish (syntax made up just now):
>
> var
> x : IIterable;
> someCollection : TSomeCollection;
> elem : TSomeElement;
>
> [...]
> x := (someCollection as IIterable).initIterator();
> while (x.hasNextElement()) do begin
> elem := (x.nextElement() as TSomeElement).next();
>
> << do something with elem >>
> end;
> x.doneIterator();
> [...]
>
> you could write:
>
> x : TSomeElement;
> someCollection : TSomeCollection;
>
> [...]
> foreach elem in someCollection do begin
> << do something with elem >>
> end;
> [...]
True, BUT... hmm, I actually have 3 BUTS
BUT 1: some form shorter syntax can be thought up for _each_ _and_
_every_ construct. That is the danger of syntactic sugar, it always looks
tempting as a shorthand. However if you indulge in it, you get Perl. If there
is a valid normal construct, and that is not extremely larger, than you
don't need it.
BUT 2: The whole purpose of iterators is that you can have multiple orders
on the same object, and get a different iterator to get a different order.
The syntax does not allow this. Typical ss.
BUT 3: I myself currently use iterators at work (modeled after decal,
except without the variant-interface stuff, because of performance) and
your example looks overly verbose;
var iter:dlightmapiterator;
iter:=lightmapstartiter(collection);
while lightmapiterateover(iter) do
<< do something with getobject(iter)>>
// no finalisation of iter necessary.
Is this so bad ? I don't see the problem
> which is imo far easier to read. (Please don't mind the word "foreach",
> I think it's only because it's already used in other languages for that
> purposes).
IMHO it sucks. Write a code template for your IDE if you really want to
spare out those 10 key strokes.
More information about the fpc-pascal
mailing list