A problem with generators

dsimcha dsimcha at yahoo.com
Wed Mar 17 14:31:04 PDT 2010


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> D currently has two ways to define a generator, the first is the older one with
opApply (And opApplyReverse):
> int opApply(int delegate(ref Type [, ...]) dg);
> The syntax of opApply is terrible: hard to remember, bug-prone, noisy,
intrusive. And this syntax doesn't even gain performance, because dmd is not very
good at inlining here.
> Several times in the past I have said that a very good syntax for this is the
Python one:
> def foo():
>   yield 5
> It's really easy to use and remember, not bug prone. This possible syntax is
about 75 times better than the current opApply:
> yield(int) foo() {
>   yield 5;
> }

The **BIG** advantage of the status quo, where the loop body is a delegate instead
of being "magic", is that it enables lots of hackish things to be done, like
running different iterations of the loop in different threads.



More information about the Digitalmars-d mailing list