Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60763 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27751 invoked from network); 6 Jun 2012 17:54:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Jun 2012 17:54:41 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@googlemail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@googlemail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.217.170 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@googlemail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:54767] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/A1-17461-F599FCF4 for ; Wed, 06 Jun 2012 13:54:40 -0400 Received: by lbgc1 with SMTP id c1so5539601lbg.29 for ; Wed, 06 Jun 2012 10:54:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=aZq8eF9D6xRKdgp2RRaqWT+WGRBHbaUyeXf57SAcY2s=; b=pIo1ufIn274eYyI1VpEw0vt/MOm5F+8WQdLXX8zBVZa062CflYtGA58lQIWqPmttNj 4220IvfU3NdVhvgaazODIRmfL0LIx6+SQWlkfIUgTLBKKh2QL6R1ZGZa7/6jZlt0h1ic VNqGgMADb1kl1eHqpDVKD0STpP3P7RxGVRz2XkeoVrA+qBZflx+UIjXIJh4rg/hH5Sck +av6bPc0KvRLuEW6CGroh9GpAbuK2yxPLUwrGcfaXBsCkcXKmOBcs10yseyr+MeBm1sO 1lIoU+1+9+Xk2Cr3cs3oGdvicBAhMMcFEz7NhrufCOvmWpo43XT52TeFECt3h1IFBRUE eHkA== MIME-Version: 1.0 Received: by 10.152.132.40 with SMTP id or8mr22604345lab.24.1339005276599; Wed, 06 Jun 2012 10:54:36 -0700 (PDT) Received: by 10.152.114.70 with HTTP; Wed, 6 Jun 2012 10:54:36 -0700 (PDT) In-Reply-To: References: Date: Wed, 6 Jun 2012 19:54:36 +0200 Message-ID: To: Benjamin Eberlei Cc: PHP internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Generators in PHP From: nikita.ppv@googlemail.com (Nikita Popov) On Tue, Jun 5, 2012 at 8:52 PM, Benjamin Eberlei 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