Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61634 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19017 invoked from network); 23 Jul 2012 02:25:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2012 02:25:13 -0000 Authentication-Results: pb1.pair.com header.from=alex.aulbach@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=alex.aulbach@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.42 as permitted sender) X-PHP-List-Original-Sender: alex.aulbach@gmail.com X-Host-Fingerprint: 209.85.160.42 mail-pb0-f42.google.com Received: from [209.85.160.42] ([209.85.160.42:42019] helo=mail-pb0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 21/D3-20357-706BC005 for ; Sun, 22 Jul 2012 22:25:11 -0400 Received: by pbbrp12 with SMTP id rp12so10845746pbb.29 for ; Sun, 22 Jul 2012 19:25:08 -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=M6GqhIdnxs8OC8BexzfXb17Fza6TR6f2UZSlDnlD7sI=; b=xdF9ghR/P5jhhhv5jH7Dxp72O10fTuXTk5m/Isld0KDbCc0q0Vo0H3gUZGgwdVis0I nJCiNyBOt/DcLRGKbDYeCEHhUmKZHly3cb7N6a6ypcXUDwBE5qnXkjLRi2d+rDCln5PU cfA6qNKBysn5M+FpLDipuAxduE/qN3EDBCcdN12i1e2EQ2LKTnaHAcA6DAHR2HSuV6pA r262LLkqX/PMEcCK7wSIf7csr6zuOGtDT6crdHXHTd+xr2+pHIR6SoSTMICU+wovPvh1 s2iAgXzGb2QqRfzeW0aZdpWhQA6j5FHyogkW2JfxiBkuNbAGNppXHOTAfSE1bO7/1jiG 2g8Q== MIME-Version: 1.0 Received: by 10.68.130.9 with SMTP id oa9mr31348718pbb.95.1343010308328; Sun, 22 Jul 2012 19:25:08 -0700 (PDT) Received: by 10.68.31.7 with HTTP; Sun, 22 Jul 2012 19:25:08 -0700 (PDT) In-Reply-To: References: Date: Mon, 23 Jul 2012 04:25:08 +0200 Message-ID: To: Nikita Popov Cc: Nikita Popov , PHP internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Re: Generators in PHP From: alex.aulbach@gmail.com (Alex Aulbach) 2012/7/20 Nikita Popov : > On Tue, Jun 5, 2012 at 7:35 PM, Nikita Popov wrote: >> Hi internals! >> The implementation is outlined in the RFC-stub here: >> https://wiki.php.net/rfc/generators > > A small progress update on this: > * Generators are now automatically detected by the presence of "yield" > instead of requiring the "*" modifier. My first thought was how could someone reading the code see, that it is a generator? I mean: The "yield" can be between 50 lines of code. How could someone see that? That a machine can do this is out of question, but humans can't see that. Why is this important? Because when I see such code as in the RFC and overlook the "yield" I would think "WTF", how could the code work like this? Why would someone not see this? Assume that the code was written and is now quite old. The original developer is gone. A new one sits on his place and now the customer says "hey, there is a bug anywhere". The developer begins to search for it, and has no clue, how he can reproduce it. Maybe he finds a part and he thinks now "Whoa, WTF" and begins to debug that. After a while he will of course find "Oh, it's a generator... just forgotten, that PHP can have that and the 'yield' is so small in the middle of the function, nobody will see that..." But he has spend some time to find that out. That's what I think will happen. I think "yield" makes it more difficult to maintain unknown code. I think "yield" alone is too less. I think the function should be explicitly marked as generator function. Stupid Suggestions: ---------------------------- generator function gen() { yield 1; } generator gen() { yield 1; } function generator gen() { yield 1; } Hm... not really satisfied with that. For me this is a little bit unlogical, that just a simple function can return an object. So, how about implementing as a class? I mean: If it is returning a class, why not implement it as class? class gen implements generator { function __generator() yield 1; } } class gen extends generator { function __generator() yield 1; } } Calling looks for me now much less "magic": $gen = new gen; $gen->next(); As maintainer I see "oh, it's a magic method", it's such an iterator thingy. Another advantage: It's not final. I could replace next() etc. with my own implementation. And it works more hand in hand with iterators. Just my very primitive first thoughts as PHP-scripts-developer, without knowing much about the implementations in other languages. :) PS: I would like to see that yield goes hand in hand with the iterator-stuff. PPS: Ok, maybe a change isn't really needed, but I think how to use this in a good manner (to avoid the problems mentioned above) should be part of the docs. -- Regards, Alex Aulbach