Op Sat, 26 Jan 2013, schreef Alexander Klenin:
var
a: array [1..5] of Integer = (1, 2, 9, 4, 5);
In my proposal, he should write:
var
v, i: Integer;
begin
for v in a index i do
Writeln(i, ' ', v);
end.
In this case I just write
for i:=low(a) to high(a) do
writeln(i,' ',a[i]);
Consider these arguments:
1) Even for simple arrays, depending on array element type,
and optimizer implementation, for-in can be more efficient since it
can avoid multiplication for element access.
True, but it shouldn't. Loop induction is such a well documented
optimization that implementation of that optimization (could be a nice job
for a student) is by far preferable over mutilating the language.
2) If "a" is a function of another complex expression returning an
array, then "for-in" may be much more efficient and compact
Again true, but again easy to avoid: temp variable, with statement, common
subexpression elimination. Again these are all preferable over mutilating
the language.
3) If "a" is an associative array (so i is a String), then the
"traditional" form of loop is not even possible.
Disagree. You need to define a mapping between an ordinal type and the
index type. This is always possible (otherwise for-in would be impossible
to implement).
4) If there is no uniform method for iterating over containers, then
each container will define its own method,
with the end result that the language will be *more* complex for the
user, who will have to remember all these methods.
This I consider a valid argument, and it affects the fundament of what a
for loop is. A for loop iterates over the keys only, while the values are
retrieved by [] syntax. This is perfectly possible even for non-numeric
keys as we have properties that allow you to do a['elephant'].
The for-in syntax violates this fundamental property by returning values,
which means there is a lack of a key. The fundamentalistic view then says
we should not have for-in in Pascal, because it changes the fundamental
property of a for-loop.
For pragmantic reasons the for-in syntax got supported. However, when
using it you discover the lack of keys. So you want to repair something
that's already broken by design.
The question becomes, which is worse? Further mutilation? Or stick to the
basics?
Daniël
_______________________________________________
fpc-devel maillist - fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel