On Tue, Jun 5, 2012 at 8:52 PM, Benjamin Eberlei <kont...@beberlei.de> wrote: > This is really awesome. Some remarks: > > 1. The * looks a bit hidden. How about reusing the yield keyword? "function > yield getLines()" I mainly chose the * modifier because this is how JavaScript (ECMAScript Harmony) does it. It also seemed to convey the idea of returning multiple values well (Cleene star). But I wouldn't have much of a problem using some other keyword. As already written in the RFC "generator" is problematic as it is not an uncommon word. I wouldn't like reusing "yield" as it doesn't make much sense semantically there (if you read "function yield xyz()" out). What seems plausible is using a separate keyword "yielding", so you have a "yielding function xyz()".
> 2. Any comments about iterator vs closure? > > function getLinesFromFile($fileName, Closure $yield) { > //.. > > while (false !== $line = fgets($fileHandle)) { > $yield($line); > } > // .. > } > Yes, using callbacks is indeed a common approach to work around the lack of generators. The problems with the callback approach (and several other alternatives) is outlined very well in the first few paragraphs of PEP 255 (http://www.python.org/dev/peps/pep-0255/). Basically the issue is that without using generators either the producer (here getLinesFromFile) or the consumer (here the main code) has to manually keep track of it's current state. Depending on the complexity of the producer/consumer this can become very hard, which can be especially well seen in the tokenizer/parser examples given in the aforementioned PEP. When using generators on the other hand the state is implicitly maintained by the VM, so the programmer doesn't have to worry about that anymore. So basically, yes, callbacks can be used for the simpler applications of generators, but with more complex problems they can quickly become very messy. Nikita -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php