Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62285 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19588 invoked from network); 20 Aug 2012 13:22:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Aug 2012 13:22:34 -0000 Authentication-Results: pb1.pair.com header.from=g.b.yahav@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=g.b.yahav@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.170 as permitted sender) X-PHP-List-Original-Sender: g.b.yahav@gmail.com X-Host-Fingerprint: 209.85.220.170 mail-vc0-f170.google.com Received: from [209.85.220.170] ([209.85.220.170:64531] helo=mail-vc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BE/A9-07742-91A32305 for ; Mon, 20 Aug 2012 09:22:34 -0400 Received: by vcbgb30 with SMTP id gb30so5668215vcb.29 for ; Mon, 20 Aug 2012 06:22:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=k66/UHkXolwsfBU48VdBwN+ClhNn82Xbqsenl3Z+uCI=; b=ctTfNWMOoCyu1s20L3htJVDtzM3Dbg4l/AGHf0vNMY2Gv4UrwRMn40HJFfNWjAuY5R WFDC0hv1RJs9tniQ+h+9/wEJz1GlVa/HTxkUGPOlLWsMe2IWMnTmZAmAcFySo/82rmlR jqQQArA/sGnMX7ExTYQkc2Wp0PnZCoYM6uqTENRyusCohZ8pOo15eENABpgQQQbf40qA 3U3Ex2CHgu39g6LcpI338fhLHqaate6fhWLwjV4fiUgoASnINHinNsBBTfZQ9R6woMA3 SPkJ7ab8SSA9l+qKsVeCIpYk4HFIYYAKNRAUQGNl2rIj0ZFZdETPadqmPdorypvm9ZDv fS0g== Received: by 10.58.65.40 with SMTP id u8mr6710832ves.47.1345468951131; Mon, 20 Aug 2012 06:22:31 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.109.230 with HTTP; Mon, 20 Aug 2012 06:22:10 -0700 (PDT) In-Reply-To: <5032321D.5070607@lerdorf.com> References: <503171CB.3090609@sugarcrm.com> <50317E51.4090305@lerdorf.com> <503225D8.4040900@gmail.com> <5032321D.5070607@lerdorf.com> Date: Mon, 20 Aug 2012 16:22:10 +0300 Message-ID: To: Rasmus Lerdorf Cc: =?ISO-8859-1?Q?=C1ngel_Gonz=E1lez?= , Stas Malyshev , Derick Rethans , Nikita Popov , PHP internals Content-Type: multipart/alternative; boundary=047d7b6d8564e3756f04c7b26659 Subject: Re: [PHP-DEV] [RFC] Generators From: g.b.yahav@gmail.com (Yahav Gindi Bar) --047d7b6d8564e3756f04c7b26659 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, Aug 20, 2012 at 3:48 PM, Rasmus Lerdorf wrote: > On 08/20/2012 07:56 AM, =C1ngel Gonz=E1lez wrote: > > On 20/08/12 02:01, Rasmus Lerdorf wrote: > >> I would still like to understand what this generator keyword would > >> actually do. I don't see how it would work. Would a function marked > >> generator somehow not be allowed to return normally or to finish and n= ot > >> return anything? How could this be enforced? I am completely against a= ny > >> keyword that is essentially documentation-only. > >> > >> -Rasmus > > Given that such function could "return several times", seems a differen= t > > enough function type to have its keyword. > > You could not decorate it and rely instead on the presence of the yield > > keyword, but parsers will thank knowing about it from the start rather > > than realising at mid-parsing that the function is a completely > > different beast. > > So how about something like this: > > generator function f() { > echo "Hello World"; > } > > generator function f() { > return 1; > } > > generator function f($arg) { > if(f!$arg) yield 1; > else if($arg<0) return 1; > else return; > } > > What does the generator keyword mean in each of these cases? Anything? > Nothing? Would I see a difference either at compile-time or at execute > time if I left it out? > > -Rasmus > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > I still don't think that we shall separate it into two different keywords, and even if so - we shall still reserve the ability to use return. Think of this case: function keywordsInFile($file, $keyword) { if ( ($han =3D fopen($file, 'r') =3D=3D=3D FALSE ) { return; // <-- this is better than starting to wrap the function with if-else } while(!feof(...)) { foreach ($keywords as $k) { if ( strpos($k, $line) !=3D=3D FALSE ) { yield $k; } } } } Though it's not the best example and I can think about more complicated - it can reveal my point - I wish to use the "return" keyword in order to stop the current function run. In many complicated sites you got many if-else cases in one function that when the statement evaluates to true/false you shall return from the function. Structure like if ( ... ) { if ( ! ... ) { if ( .... ) { if ( ... ) { yield $v; } } } } is ugly and make the program harder to read than if ( ! ... ) return; if ( ... ) return; if (! ... ) return; if ( ! ... ) return; yield $k; --047d7b6d8564e3756f04c7b26659--