foreach syntax, std.mixin

dsimcha dsimcha at yahoo.com
Sun Nov 8 13:43:34 PST 2009


== Quote from Philippe Sigaud (philippe.sigaud at gmail.com)'s article
> > dsimcha wrote:
> > Makes me wonder why noone thought of this until now, or maybe someone did
> > and I
> > forgot.  How's:
> >
> > foreach(fooElem, barElem; unpack(zip(foo, bar))) {}, or:
> >
> > foreach(i, elem; enumerate(chain(foo, bar))) {} ?
> >
> >
> Can that be done for more than two ranges? The syntax is so nice, I'd be
> deligthed to have that.

Hot off the press and VERY prototype-ish:

Code:
http://pastebin.com/m2087e524

Docs:
http://cis.jhu.edu/~dsimcha/unpackEnumerate.html

Does this look like a good addition to std.range?  The elegance of it is it solves
the problem of providing syntactic sugar to ranges w/ zero ripple effects either
in the compiler or in the rest of Phobos.  I'll file it somewhere more official
after people review it a little and refine the idea, but I definitely think
something similar to this has a legit place in std.range.

If you're wondering how unpack works and don't want to grovel through all the
code, it's tons of string mixin magic.  That's about the only way I was able to
make it work.



More information about the Digitalmars-d mailing list