[fpc-devel] for-in-index loop
Alexander Klenin
klenin at gmail.com
Fri Jan 25 00:26:37 CET 2013
On Fri, Jan 25, 2013 at 9:36 AM, Michael Van Canneyt
<michael at freepascal.org> wrote:
>>> If you want full fledged iterators, use classes.
>>
>> Please provide example of your suggestion for the case in the wiki.
>
> I don't need to provide *anything*.
Of course you do not, this is why I said "please" :)
However, it is impossible to have a meaningful discussion without such
an example,
so could you please indulge me?
> Out in the real world, you'll need to prove that this actually is useful and
> will be used a lot.
> "For in" is debatable by itself. It is syntactical sugar, it provides
> nothing that for a:=b to c does not give.
The whole concept of high-level programming language is to add syntax
sugar to the assembly/machine code.
So the question is, indeed, how often this particular sugar would be used.
As an example, I have made a quick survey of TAChart code, since it is
familiar for me and easily available
for everybody here.
TAChart (with demoes) contains about 410 "for" loops.
Of those, 82 are already converted to "for-in". Of the rest, about 200
are iterating over whole containers --
others are partial iteration, coordinate iteration, etc.
Of these 200, there are three reasons for not using for-in:
1) Oversight. I convert code are I work on it, so some loops just wait
their turn.
2) Modifying the container (I have another extension proposal for
these -- "for var i in a" syntax).
3) Requiring loop index -- those are the ones we are interested now.
After a quick look, I estimate about 30 such places.
So from the above study we can estimate that 5-7% of all "for" loops,
and 20-30% of "for-in" loops would benefit from the proposed extension.
> Now you propose to extend this "sugar" syntax with something even more
> exotic, which I have not even encountered in other languages.
PHP:
foreach($array as $index => $value)
...
Python:
for i, item in enumerate(seq):
...
Ruby:
set.each do |item,value|
...
end
Go:
for index, value := range someCollection {
...
}
Objective C:
[items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop)
{
…
}];
JavaScript:
array.forEach(function(value, index) {
...
});
Java indeed does not have it.
> Perl where every possible idea of every
> contributor that popped up on a blue monday was implemented and incorporated.
Why do you think so? Just look at the in perl-porters mail archive --
they are very far from "implement every possible idea", I assure you.
> Go and propose your idea to the gcc team as an extension to C. let's see how
> far you get...
C is not a reasonable comparison, since it is much lower level language.
I could also go and propose to include classes, sets, strings -- they
all do not make sense in C.
Now, C++ is a much better comparison target.
Unfortunately, current pace of evolution for C++ language is incredibly slow.
It only for for-in last year, in the latest version of the standard.
I think it is reasonable to assume that next version of C++ might
include "for-in-index" syntax.
Unfortunately, I also think it is reasonable to estimate the timeframe
for that version as 5-7 years at least.
--
Alexander S. Klenin
More information about the fpc-devel
mailing list