Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73834 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22758 invoked from network); 28 Apr 2014 20:30:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Apr 2014 20:30:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.174 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.214.174 mail-ob0-f174.google.com Received: from [209.85.214.174] ([209.85.214.174:49228] helo=mail-ob0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 80/A0-19537-E6ABE535 for ; Mon, 28 Apr 2014 16:30:39 -0400 Received: by mail-ob0-f174.google.com with SMTP id gq1so7872737obb.5 for ; Mon, 28 Apr 2014 13:30:36 -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=MZdx3bRWoqbjgqL42EvlQyTwLxOBRECWF3DIxQ8wozY=; b=VlsyaVR0KpA+FNyOOHF1BGt9LZPmwh2Flzio9v7L+vIA8/OnpPdOgP21LqX/RPzvDS rjzBStEOvePXSPgRWnhP1YLoR/d4o2C5nnzGKSDIV1kyYXaqF0qRSSL712QWV3Ce8m0g +eBkUiaxm7HX1Y/s0W+F31xGrHNFEZqt1NODBZLVEDSXDSKJmVrbv71FdACUlpLIE1dU efzP5IjgicJ71c6XRTUq8Gq5CCq0tlNPLiBjurkuJMl5QBn48eUKPwHu2KlkWlquioF2 HKvqmTRQ+omlOUQLrrBj8d9E6s1LBMZ/LcrkdijsKYQr5j4LgZIeE45Ry/yIx5MzezXb zVug== MIME-Version: 1.0 X-Received: by 10.60.17.132 with SMTP id o4mr24001820oed.34.1398717036078; Mon, 28 Apr 2014 13:30:36 -0700 (PDT) Received: by 10.76.77.100 with HTTP; Mon, 28 Apr 2014 13:30:35 -0700 (PDT) In-Reply-To: <535E9AED.2080904@garfieldtech.com> References: <535E9AED.2080904@garfieldtech.com> Date: Mon, 28 Apr 2014 14:30:35 -0600 Message-ID: To: Larry Garfield Cc: internals Content-Type: multipart/alternative; boundary=089e01294d0c13aca904f8203071 Subject: Re: [PHP-DEV] [RFC] Return Type Declarations From: morrison.levi@gmail.com (Levi Morrison) --089e01294d0c13aca904f8203071 Content-Type: text/plain; charset=UTF-8 On Mon, Apr 28, 2014 at 12:16 PM, Larry Garfield wrote: > On 4/24/14, 10:59 AM, Levi Morrison wrote: > >> My dear Internals friends, >> >> I have spent the last month drafting an RFC that allows return types: >> https://wiki.php.net/rfc/returntypehinting >> >> Notable items: >> - Differences from past RFCs are summarized in the RFC: >> https://wiki.php.net/rfc/returntypehinting#differences_from_past_rfcs >> - The patch includes basic opcache and reflection support. It also has >> phpt tests. >> - Informal performance tests indicate that the patch did not fubar >> performance; if desired a more detailed test can be conducted before >> voting. >> - This RFC does not add, modify, or remove keywords at all; this >> notably >> excludes type-hints for scalars. Supporting scalar type declarations is >> outside the scope of this RFC; if you are interested in supporting scalar >> type declarations please discuss it elsewhere. >> >> As a friendly reminder, everyone on this list is interested in developing >> a >> better PHP and the definitions of 'better' vary from person to person. >> Please be civil and constructive while discussing this RFC. Thank you! >> >> Some thanks, regardless if the RFC is accepted: >> - For providing a patch: Joe Watkins. >> - For helping me iterate on RFC drafts: Bob Wienand, Nikita Popov and >> Anthony Ferrara >> - For previous RFCs on this topic: Felipe Pena and Will Fitch. I >> gleaned >> valuable knowledge from your proposals and the discussion around them. >> > > Simple, targeted, focused, I like. I very much like the NULL handling, as > it means as a caller I have to do less work. > Thank you. > One bit strikes me as odd. In the invalid examples, you have this example: > > function foo(): array { > yield []; > } > > which generates this error: > > Fatal error: Generators may only yield objects, array is not a valid type > in %s on line %d > > Except Generators can totally yield arrays currently: > > function foo() { > yield []; > } > foreach (foo() as $x) print_r($x) . PHP_EOL; > > The above works perfectly on 5.5.10, where I just tested it. > > So what's with that error condition? Clearly from the RFC we can specify > array as a return type, so I don't know why it calls out generators as > specially limited. > > As I write that I suspect it's because generators actually "return" an > interator object, and so the return type of the generator is being > interpreted relative to the construction, not iteration, of the generator. > That seems like a likely source of confusion, as I would, as a developer, > expect to be specifying the type that gets returned from iteration, not > from construction, so that my foreach loop (or similar) can rely on the > type being returned. > The type hint is purposefully not the type of the value returned; a generator always returns a generator. I will clarify this in the RFC and maybe adjust the wording in the error message as well to better indicate this. > I think this part warrants further consideration. The rest of the RFC I > fully look forward to using. > Special casing a generator's return type would be an inconsistency in the design. If you var_dump() something that returns generator it tells you it is a generator; this should match the type of the return value and anything else is unexpected. As an example, using a generator to fulfill an Iterator return type should succeed; special casing the Generator would complicate the code to achieve this. Special casing the generator would also infringe on other future ideas, such as adding generics to the language. All in all, special casing generators doesn't make sense. As far as the order of keywords in a closure, what order does Hack use? > The rest of the syntax seems based on it, so it seems sensible to follow > suit. (I couldn't find that information from 60 seconds on hacklang.org, > but it may be there.) At least in the 2.4 branch of HHVM (which I have installed) they put it before the use statement. Now that I have thought about this more, this is a poor decision: * use statements are basically parameters at construction time instead of runtime * putting the return type before the use statement looks really ugly I will attempt to contact the HHVM team (perhaps SaraG) to see if there was a technical reason for it. --089e01294d0c13aca904f8203071--