Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68094 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43931 invoked from network); 11 Jul 2013 19:31:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jul 2013 19:31:27 -0000 Authentication-Results: pb1.pair.com header.from=florinpatan@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=florinpatan@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.43 as permitted sender) X-PHP-List-Original-Sender: florinpatan@gmail.com X-Host-Fingerprint: 209.85.219.43 mail-oa0-f43.google.com Received: from [209.85.219.43] ([209.85.219.43:37873] helo=mail-oa0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8E/A3-03322-B080FD15 for ; Thu, 11 Jul 2013 15:31:24 -0400 Received: by mail-oa0-f43.google.com with SMTP id i7so11690944oag.2 for ; Thu, 11 Jul 2013 12:31:20 -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=aQqdrNA4K/B2f3Ic8oXxQQPvgYs5hC2nDx56hTTGd1o=; b=kFzPsu0sq9nU8QXril6OdXIPrcgtGlOVh/002aZWn/aQu5ctRtjzZ9096yokM6MdvP SLl9DrjEyVc4Hpzc8rCRgn3piiptk8gnCbjBgqX1CqDIrvA7DT5wHcZbSQlC89olSKdh WE119GWdKSjIC6jtsC7tNZery8MqMCjRNnkVYGs3YipaDYf0gJ4UZ3oFlHgC5BXUHBYn Ya7nY5xlC2F9duJfSyfEvnE+7c3IZGoZ3Y8IeWXVfguvksnGfA1QmCE9NgW0Zl9pNGVp rv4iBXFi8T0D55QnpahuHaxLF0x7I9VTtk6XKkavjBm73KUR+vyJPwggOL5QvfxWyxLA CA/A== X-Received: by 10.60.37.233 with SMTP id b9mr32732952oek.61.1373571080075; Thu, 11 Jul 2013 12:31:20 -0700 (PDT) MIME-Version: 1.0 Received: by 10.76.18.161 with HTTP; Thu, 11 Jul 2013 12:30:50 -0700 (PDT) In-Reply-To: References: <1372258135.2410.22.camel@guybrush> Date: Thu, 11 Jul 2013 21:30:50 +0200 Message-ID: To: Sara Golemon Cc: RQuadling@gmail.com, PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Request for comments - new PHP feature: return typing From: florinpatan@gmail.com (Florin Patan) On Wed, Jul 10, 2013 at 8:30 PM, Sara Golemon wrote: >> With regard to any sort of hinting, an issue of 'one of' return types >> would need to be addressed (I hope). >> >> function foo(SomeClass $bar, int $baz) : bool, SomeClass {} >> >> Allowing for a failure (False) or a success (SomeClass). >> >> We (HHVM) deal with failure alternates by way of a "nullable" syntax: > > function foo(SomeClass $bar, int $baz) : ?SomeClass {} > > Means the function returns SomeClass or null. null being the "failure" > case. > > We also do "mixed" for anything (basically, no type hint, but explicitly > stated), and "numeric" for a less aggressive float/int/numeric-string > (though that's more specific to arg types than return types). > > > >> >> I think it would useful to have a Null hint (like C's void I suppose). >> More to do with explicitly saying no return type, rather than saying >> nothing which means an undefined return type. >> >> function foo(SomeClass $bar, scalar $baz) {} /** you can return >> something, but who knows what will happen with it. **/ >> vs >> function foo(SomeClass $bar, scalar $baz) : Null {} /** This method does >> not return anything and to do so would be wrong. **/ >> >> > function foo() : void {} is our syntax. A null return value is an actual > thing (a null thing), whereas no return is more explicit. I know that > `return;` gets translated into `return null;` anyway, but we also look at > the caller side (return_value_used) and warn about "attempt to use value > from a function returning void" like C/C++ do. > > > Another advantage of scalar is the ability to stop scalar only functions >> from receiving objects, arrays, etc.. Currently no way to inhibit that >> without type testing in the function. >> >> We don't currently do "any scalar", though we do have "numeric" (as > mentioned above). I agree with you that "scalar" would be a nice meta-type. > > > >> And, if PHP evolves to allow for __toScalar(), then an object can >> represent itself as a scalar for those situations where the scalar type >> hint is required. I think this may be a bit much initially though (unless >> some clever bod has already worked all this out). Being able to cast an >> instance from type A to type B via __autocast_XXXX where XXXX is the >> resultant type ... ha ha ... OK that really is just pushing things. Maybe. >> >> Going off-topic here, but I worry that __toScalar() would collide in a > messy way with __toString() (and other, future __toXXXX() behavior). Not > severely against it, mind you, but I think we (PHP) need to be cautious > about going down that road. > > -Sara > > P.S. - Your reply only went to me, but I assume you wanted to include > internals@ so I put them back in the To: line. Thanks for your input Sara, everything sounds very interesting. In all fairness I haven't looked at HHVM but I from what you described about this particular feature sounds good. Should I conclude that there's enough interest for this feature and move from a gist to a RFC? If so, I would definitely need someone to help out with the implementation. Best regards, Florin ---- Florin Patan https://github.com/dlsniper http://www.linkedin.com/in/florinpatan