August 01, 2003
While we're talking about ideas for the "foreach" keyword, I'd like to chip in something that concerns me a bit.

Foreach has its roots in perl (doesn't it?) where it only has to iterate over arrays. And, while I'm a big fan of arrays, I'd prefer to use the best type of data structure for a given situation. And if I'm going to use a foreach to iterate over the elements in a custom templated class, an intrinsic foreach won't be able to do the job. An intrinsic foreach iterator would have no idea how to access the next element of a class, not to mention how to find it in the most efficient way.

In particular, I'm thinking about a linked-list implementation that I just finished writing. Let's say I want to iterate over all of the elements in my list. How would an intrinsic foreach know how to find the elements of my list? Even if it could somehow figure out how to get the ListNode elements out of the LinkedList, it would probably be abysmally slow, since it would probably only be able to use getElement(int) rather than getNextElement().

As the implementor of the LinkedList class, I could create a __foreach function that would return the next element of the list, without having to hop through all of the list links for each element.

So, what I'd like to see is something that looks like this:

***********************************************

instance TLinkedList(ListNode).LinkedList myList;

ListNode myLlistNode1 = newListNode();
ListNode myLlistNode2 = newListNode();
ListNode myLlistNode3 = newListNode();

myList.appendElement(myLlistNode1);
myList.appendElement(myLlistNode2);
myList.appendElement(myLlistNode3);

foreach (ListNode x from myList) {
doStuff(x);
}

***********************************************

To do so, I'd better have a foreach handler (an iterator, I guess) in my LinkedList class that returns a ListNode object. If I haven't specified a foreach handler that meets those requirements, the code shouldn't compile.

In the case of my LinkedList class, I would probably just make the foreach handler pass on a call to getNextElement until it encountered a LinkedListException.

Of course, dmd should have built-in foreeach handlers for instrinsic data structures like static arrays, dynamic arrays, associative arrays, strings (for iterating over the chars in the string).


August 09, 2003
"BenjiSmith" <BenjiSmith_member@pathlink.com> wrote in message news:bge3dp$n5c$1@digitaldaemon.com...
> While we're talking about ideas for the "foreach" keyword, I'd like to
chip in
> something that concerns me a bit.
>
> Foreach has its roots in perl (doesn't it?) where it only has to iterate
over
> arrays. And, while I'm a big fan of arrays, I'd prefer to use the best
type of
> data structure for a given situation. And if I'm going to use a foreach to iterate over the elements in a custom templated class, an intrinsic
foreach
> won't be able to do the job. An intrinsic foreach iterator would have no
idea
> how to access the next element of a class, not to mention how to find it
in the
> most efficient way.
>
> In particular, I'm thinking about a linked-list implementation that I just finished writing. Let's say I want to iterate over all of the elements in
my
> list. How would an intrinsic foreach know how to find the elements of my
list?
> Even if it could somehow figure out how to get the ListNode elements out
of the
> LinkedList, it would probably be abysmally slow, since it would probably
only be
> able to use getElement(int) rather than getNextElement().
>
> As the implementor of the LinkedList class, I could create a __foreach
function
> that would return the next element of the list, without having to hop
through
> all of the list links for each element.
>
> So, what I'd like to see is something that looks like this:
>
> ***********************************************
>
> instance TLinkedList(ListNode).LinkedList myList;
>
> ListNode myLlistNode1 = newListNode();
> ListNode myLlistNode2 = newListNode();
> ListNode myLlistNode3 = newListNode();
>
> myList.appendElement(myLlistNode1);
> myList.appendElement(myLlistNode2);
> myList.appendElement(myLlistNode3);
>
> foreach (ListNode x from myList) {
> doStuff(x);
> }
>
> ***********************************************
>
> To do so, I'd better have a foreach handler (an iterator, I guess) in my LinkedList class that returns a ListNode object. If I haven't specified a foreach handler that meets those requirements, the code shouldn't compile.
>
> In the case of my LinkedList class, I would probably just make the foreach handler pass on a call to getNextElement until it encountered a LinkedListException.
>
> Of course, dmd should have built-in foreeach handlers for instrinsic data structures like static arrays, dynamic arrays, associative arrays, strings
(for
> iterating over the chars in the string).

You're right. I have plans to do this, just haven't done it yet.