Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62126 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50945 invoked from network); 13 Aug 2012 02:49:21 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Aug 2012 02:49:21 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.49 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.49 mail-qa0-f49.google.com Received: from [209.85.216.49] ([209.85.216.49:55122] helo=mail-qa0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/A1-00812-F2B68205 for ; Sun, 12 Aug 2012 22:49:19 -0400 Received: by qafk1 with SMTP id k1so2170893qaf.8 for ; Sun, 12 Aug 2012 19:49:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=kU4//r1zI33EFv4TfymT00KL4i8o+7gwcSsAFZyBAVg=; b=aI9wQdDOwZKsnmcNtGC0JWFzQZq4bjwyiXczlzpqtXJ+2D8ZX2Rrz9AGwixsaJUu+2 HQ1U6fswXRmYPMaj6nTSCn+14a7p5emDz2KkbxzbzhS+juF5E6uFT7Es2hbFyxDvgWEn Jv3xDYCuONTsO5PZP5Z5gykzjuVOV2ABdtrUgoIMIpnvJTB+hGCRb5zwyqNeiEFTdfcm vRwIPWsXYR6WkZEEJlS5YCbrbpT/fev86Z+HkT7ZpEh3QwC056bvvf3Y/Ke0gHmz3s4X GKx6hZcNfHi7nqWAD9svWVlF89jS4E4wPsm3A0n1n4BkQO+KfjuvAgjyLo0x1nr6D2DN haww== MIME-Version: 1.0 Received: by 10.229.105.166 with SMTP id t38mr5539527qco.136.1344826156200; Sun, 12 Aug 2012 19:49:16 -0700 (PDT) Received: by 10.229.54.213 with HTTP; Sun, 12 Aug 2012 19:49:16 -0700 (PDT) In-Reply-To: <50280D37.10109@moonspot.net> References: <50280D37.10109@moonspot.net> Date: Sun, 12 Aug 2012 22:49:16 -0400 Message-ID: To: Brian Moon Cc: Nikita Popov , PHP internals Content-Type: multipart/alternative; boundary=00235429d19053357e04c71cbdd3 Subject: Re: [PHP-DEV] [RFC] Generators From: ircmaxell@gmail.com (Anthony Ferrara) --00235429d19053357e04c71cbdd3 Content-Type: text/plain; charset=ISO-8859-1 Brian, On Sun, Aug 12, 2012 at 4:08 PM, Brian Moon wrote: > Hi Nikita, > > I admit, I have ignored these threads as there was no RFC. So, some of > this may have been covered. > There was an RFC in those posts... It was just being iterated over. > Do you have a good example usage other than a file? I don't find > fopen/fgets/fclose all that complicated. What are the other valid use cases > for such a thing? > Here's a quick set of examples: http://blog.ircmaxell.com/2012/07/what-generators-can-do-for-you.html > Also, not allowing rewinding is unintuitive for something that is an > iterator in PHP. If I can foreach() it and I can call next() on it, I > expect to be able to reset() it as well. IMO, you would need to issue a > FATAL PHP error if that simply is not allowed. Or you have to have a second > syntax for what to do in that case. At that point, you are implementing > Iterator. > I partially agree. rewinding the generator might be possible, but it's hard to tell in those cases. It's hard to tell if resetting it is even possible from the code level. So I'm pretty much OK with living with that restriction for the time being... > While I am glad that PHP has borrowed syntax from many languages, I find > the yield keyword to be very WTF when I first saw it. It is also poorly > explained in your RFC. You use terms like "sending and receiving". That > does not say "returns from function execution" to me. I had to basically > figure it out from the code examples. > It's absolutely something that takes getting used to. But it's also quite intuitive once you think about it. You're not "returning" back to the parent scope (exiting your scope), you're "yielding" a value to the parent scope, expecting to continue on later (think of it as a stop light where you let the other code run for a while until you go to the next one). > Lastly, because you cite needing this for sending data to PHPUnit, I think > this is a user land problem and not a core problem. In about 5 minutes I > implemented a reusable Generator object that does exactly what you need. > http://pastebin.com/V336rEpR At least, it does what your examples show > you need. Again, file IO is very easy and perhaps that example does not > explain your true need very well. > Well, there's one thing that should be made clear. There's nothing (and I mean that) that generators provide that you can't do already. You can build iterators that do exactly the same thing. The trick that generators provide you is a really short, and really easy way of implementing iterators. You don't need to worry about maintaining state or anything like that. All you need to do is code it how you would normally, and replace your executing code with a yield. No more needing to split out and maintain state in weird ways, or using literally hundreds of lines of code (because of the boilerplate involved) where a dozen would do... Anthony --00235429d19053357e04c71cbdd3--