Walter Bright [off-list ref] writes:
void foo(int array[10])
{
for (int i = 0; i < 10; i++)
{ int value = array[i];
... do something ...
}
}
to:
void foo(int[] array)
{
foreach (value; array)
{
... do something ...
}
}
takes a lot of frankly unnecessary things away, each of which is a
potential source of error when maintaining the code.
The problem is a toy problem: in real applications, you'll need to
access several data structures using the same index, and you'll need
to be able to assign index values to temporary variables and so on.
So being able to hide the type of an index in one very specific
application (looping through a single array completely) at one place
is not going to buy you much.
Anyway, D is pretty much irrelevant as a perspective for git, so you
should take it to a language advocacy group.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
David Kastrup wrote:
The problem is a toy problem: in real applications,
Necessarily, to make an example suitable for a n.g. post, I ruthlessly
cut down the size of it. This can have the inadvertent effect of making
it appear trivial.
you'll need to
access several data structures using the same index, and you'll need
to be able to assign index values to temporary variables and so on.
The index is available:
foreach (index, value; array)
{
writefln("array[%s] = %s", index, value);
}
and it isn't necessary to worry about what the correct type for index
is, as it is inferred.
So being able to hide the type of an index in one very specific
application (looping through a single array completely)
foreach'ing over a subset (i.e. slice) of an array:
foreach (value; array[5 .. $])
... loop from 5 to the end ...
at one place is not going to buy you much.
Experience with foreach in real code shows that the for loop is what
becomes a rarity. Simple as it is, foreach is one of the best liked
improvements D has. And I speak as one who has written so many for loops
that spewing out:
for (int i = 0; i < 10; i++)
is a 'finger' macro for me, i.e. my fingers blit it out without even
thinking about it.
> Anyway, D is pretty much irrelevant as a perspective for git, so you
> should take it to a language advocacy group.
I wished to answer your specific comments in this post.