Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60775 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91816 invoked from network); 7 Jun 2012 08:46:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Jun 2012 08:46:22 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.22 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.22 smtp2.ist.utl.pt Linux 2.6 Received: from [193.136.128.22] ([193.136.128.22:55851] helo=smtp2.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4A/C4-60259-D5A60DF4 for ; Thu, 07 Jun 2012 04:46:21 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 872FF7000447; Thu, 7 Jun 2012 09:46:17 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp2.ist.utl.pt ([127.0.0.1]) by localhost (smtp2.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id 25GNEg-GLrh1; Thu, 7 Jun 2012 09:46:17 +0100 (WEST) Received: from nebm.ist.utl.pt (unknown [IPv6:2001:690:2100:4::58:1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 041707000435; Thu, 7 Jun 2012 09:46:16 +0100 (WEST) Received: from localhost ([127.0.0.1] helo=nebm.ist.utl.pt) by nebm.ist.utl.pt with esmtp (Exim 4.72) (envelope-from ) id 1ScYMC-0005Nj-SH; Thu, 07 Jun 2012 09:46:16 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 07 Jun 2012 10:46:16 +0200 To: Nikita Popov Cc: internals PHP Organization: =?UTF-8?Q?N=C3=BAcleo_de_Engenharia_Biom=C3=A9dica_do_Insti?= =?UTF-8?Q?tuto_Superior_T=C3=A9cnico?= In-Reply-To: References: <5361275d0179dd0f3061ceabf7bde9af@nebm.ist.utl.pt> Message-ID: <3dfb58cfcdefa75813fa670231040c7b@nebm.ist.utl.pt> X-Sender: glopes@nebm.ist.utl.pt User-Agent: RoundCube Webmail/0.5.3 Subject: Re: [PHP-DEV] Generators in PHP From: glopes@nebm.ist.utl.pt (Gustavo Lopes) (Sorry, I pressed something that sent the message prematurely) On Thu, 7 Jun 2012 01:10:52 +0200, Nikita Popov wrote: >> current() and key() should return false when !valid() > > Are you sure about that? The Iterator::current() docs don't specify > anything, but the Iterator::key() docs say that it should return NULL > on failure. Checking on the first spl class that came to my mind > SplFixedArray also returns NULL when it is out of elements. My bad. I was under the impression the semantics were similar to those of next(), key(), etc. Instead the docs say under current() that the function can return anything, under key) that it returns NULL on failure (and issues an E_NOTICE) and for next() that "the return value is ignored" -- whatever that means; I'll interpret it as saying anything can be returned. I'm not sure how correct this documentation is, though. >> next() is underspecified. > Not sure what exactly you mean. next() really doesn't do much more > than resuming the generator. What should I add additionally about it? Sorry for not having been clearer. I mean you say "Resumes the generator (unless the generator is already closed)", but you don't specify what it returns (though apparently Iterator does not require you to specify anything specific) and you don't say what happens when theiterator is already closed. >> In fact, if you can't implement Iterator in full, you should >> implement Traversable instead. > Not sure whether the lack of rewinding behavior really disqualifies > it > to use the Iterator interface. I think having the internal iteration > methods exposed is quite handy, especially if you consider their use > as coroutines, where you often want to call the iteration interface > manually and not using a foreach loop. > Well, the Iterator::rewind() docs say that rewind() rewinds the iterator. If this is not possible, then Iterator should not be implemented. See http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.8 In the same order, your options are: 1. As changing Iterator is not possible, have a new interface, say, BasicIterator from which Iterator will inherit without rewind(). 2. Make all the Generators rewindable (possibly by fetching a new generator each time). 3. Do not implement Iterator. (By the way, if you implement Traversable, one you can always use the unsafe IteratorIterator to get access to the methods) > * Exceptions can be thrown from generators as always. > * Not sure what you mean by "generator returns function". What would > be that problem with that? I mean how do you deal with "return $foo" in the body of the generator? Does it work like a final yield? Is the return value ignored? > * Generators can be methods, as shown in the example in the > Introduction section. Closures can also be generators. Can other callable classes be generators? Are you planning on any internal API for functions to implement generators (though I really haven't thought how that would work)? -- Gustavo Lopes