foreach, an analogy

rm roel.mathys at gmail.com
Thu Oct 19 12:07:30 PDT 2006


Walter Bright wrote:
> Gregor Richards wrote:
>> Walter Bright wrote:
>>> The C++ iterator approach has a serious problem: collections need to
>>> be linearized. This is not reasonable for some types of collections,
>>> such as a binary tree, which really wants to be traversed in a
>>> recursive descent fashion, rather than linearly.
>>
>> A slight modification can fix that:
>>
>>  for (auto bar = foo.begin(); !(bar is null); bar = bar.next())
>>
>> Anyway, my point was merely that 'foreach' is not analogous to the
>> too-small-door, since it's merely an alternative to 'for' :)
> 
> How do you make for work with the following data structure:
> 
>     struct Tree
>     {    Tree* left;
>         Tree* right;
>         int data;
>     }
> ?
I'd say, the .next() takes an argument, which defines the policy by
which the tree is traversed. No argument in the call would mean a
default policy, like depth-first or something. Other policy could be
provided, and custom policies could be defined and given as a parameter.

my 5 cents though,
roel



More information about the Digitalmars-d-announce mailing list