Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62244 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93081 invoked from network); 18 Aug 2012 15:57:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Aug 2012 15:57:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-lpp01m010-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:56242] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5C/C8-31572-E4BBF205 for ; Sat, 18 Aug 2012 11:57:03 -0400 Received: by lahl5 with SMTP id l5so2699895lah.29 for ; Sat, 18 Aug 2012 08:56:56 -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=diMjVN0toaoXSsJ5vwfip8/anw49VS7gH1lU+vYgibw=; b=CfwcG2CeLpFCGbWa6ohH18QieoXCHnfBSdjwl/joizqB4vNjbRAbCXS9ruR+JyPA9a srtrobtyP2XBXH/ZOxFS2Dfkdg1W0pzrwb6gGFu0yVvn8h60HcZL8OMpJQ4OmNWO32A1 xb3SeoK2/g3SPB38A7VSq+v7Nr7aOrW5xzfmBdnHtGM0i0aUqhGxvei2TdLCbTPr6icB 7skBqL08n9Lt+8xr+1bTB3w3SSkai5iRWkbybWSuyxPkO3eiGFpj/aoQUOPQVXXuVLV9 Eb3Qzm6dk/YNRbFvRY3pwFZLnkdWyMi0VhZrk8vXlMU/bXckViK1EeUaltLjMOBzrHW7 zS3A== MIME-Version: 1.0 Received: by 10.152.110.46 with SMTP id hx14mr8411360lab.21.1345305416484; Sat, 18 Aug 2012 08:56:56 -0700 (PDT) Received: by 10.152.122.51 with HTTP; Sat, 18 Aug 2012 08:56:56 -0700 (PDT) In-Reply-To: <13989.212.183.128.50.1345299152.squirrel@mail.lsces.co.uk> References: <13989.212.183.128.50.1345299152.squirrel@mail.lsces.co.uk> Date: Sat, 18 Aug 2012 17:56:56 +0200 Message-ID: To: lester@lsces.co.uk Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] [RFC] Generators From: nikita.ppv@gmail.com (Nikita Popov) On Sat, Aug 18, 2012 at 4:12 PM, wrote: > Since this is yet another area where 'one does not have to use it if one > does not want to' ... FLAGGING to the other users that a function is a > 'special one' rather than just a normal function with a generator function > seems to me just a necessity? HAVING to work through a functions code > simply to see if it contains a 'yeild' so as to understand that it's not > going to give a normal return seems insane? Alright the function can be > properly documented with a docblock, but if it isn't ... I don't understand this argument. Generator functions are transparent to the user. You use a generator function just like you would use a function that returns an array. From a user point of view it does not matter whether getLinesFromFile() is just a function returning an array, or whether it is a generator (or maybe even returns a hand-implemented iterator). It's just all the same. The fact that the function uses `yield` internally is just an implementation detail, not something that has to be part of the public API. Actually you can swap between an array and generator implementation just by replacing one line in the function body (yield <=> $array[])... >>> generator function getLinesFromFile($fileName) { >>> if (!$fileHandle = fopen($fileName, 'r')) { >>> return; >>> } >> >>> There is an existing generator implementation in HipHop PHP, which >>> uses automatic-detection. Using the asterix modifier would break >>> compatibility. >> >> This should not be a concern, sure, it's annoying for the hiphop >> developers but they chose to copy and then *chance* the PHP language for >> their own effect. >> >>> yield: Yields the value null with an auto-incrementing integer key. >> What is the usecase for this? > I can see some interesting porting problems with this ... one of the stock > changes when moving from a MySQL only setup to support other databases is > to remove the auto-increment magic, and replace it with proper sequence > code prior to inserting. I can see translating a 'MySQL' geared generator > into a more general one as being impossible if some tricks like this are > used. Generators are supposed to act very similar to array-returning functions, so they also have similar key semantics. If you do $result[] = $foo; that will behave the same as doing yield $foo;. Both will use an auto-incrementing key. Nikita