On Mon, Jan 23, 2012 at 01:16:29PM -0800, H. S. Teoh wrote:
> On Mon, Jan 23, 2012 at 03:32:07PM -0500, Matt Soucy wrote:
> > So I was messing around with some code I've been writing recently,
> > and I wanted to use a foreach on a struct as if it were an
> > associative array. The problem is, I can't find any information on
> > how to do that.
[...]

Oops, missed the part about associative arrays. For that, you can simply
add more arguments to the delegate, for example:

        struct Test {
                int opApply(int delegate(ref int, ref int) dg) {
                        auto ret = 0;
                        for (auto i=0; ret==0 && i<5; i++) {
                                auto j = i+10;
                                ret = dg(i, j);
                        }
                        return ret;
                }
        }

        import std.stdio;
        void main() {
                Test t;

                foreach (n, m; t) {
                        writeln(n, " ", m);
                }
        }


T

-- 
"I suspect the best way to deal with procrastination is to put off the
procrastination itself until later. I've been meaning to try this, but
haven't gotten around to it yet. " -- swr

Reply via email to