Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88800 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48205 invoked from network); 14 Oct 2015 23:19:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Oct 2015 23:19:19 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.27 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.27 out3-smtp.messagingengine.com Received: from [66.111.4.27] ([66.111.4.27:58645] helo=out3-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 24/57-33697-4F2EE165 for ; Wed, 14 Oct 2015 19:19:17 -0400 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 512B5200F7 for ; Wed, 14 Oct 2015 19:19:14 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Wed, 14 Oct 2015 19:19:14 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=Y6vy/ZJ250iyTRH NJcgHEaJRXck=; b=bKU9UJSVJqtHItfmocSzO4HRG6DtjjZBZhb3MhnnEDAwqo0 PGeRF65L3QwFJ5GEwvf0C7S5F++zujZbrDJXEjFCWjj+DU4XqQKW9wH3ZI6zJuwY Fr1oyxPZ8WjZZyXBmVTefWp9KMf4s4xVtVGIKtQX0xrMkD4jNSn5sBTfUg0M= X-Sasl-enc: ynHTCMYK9K6jOyHwRR3SsPA7AAOBKlcxxWSHCZnITpJJ 1444864754 Received: from [192.168.42.5] (c-73-208-148-59.hsd1.il.comcast.net [73.208.148.59]) by mail.messagingengine.com (Postfix) with ESMTPA id 02E0D6800A3 for ; Wed, 14 Oct 2015 19:19:13 -0400 (EDT) To: internals@lists.php.net References: <0A.C2.33697.6AECE165@pb1.pair.com> <561ED07E.4060809@gmail.com> Message-ID: <561EE2F1.7050305@garfieldtech.com> Date: Wed, 14 Oct 2015 18:19:13 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: =?UTF-8?Q?Re:_[PHP-DEV]_[RFC]_Void_Return_Type_=28v0.2=2c_re=c3=b6p?= =?UTF-8?Q?ening=29?= From: larry@garfieldtech.com (Larry Garfield) On 10/14/2015 06:00 PM, Andrea Faulds wrote: > Both you and Stas have said this, but it's only true if we solely > consider C-like languages. Other languages do different things. In the > PHP manual, Hack, TypeScript, ActionScript, and most likely other > languages (these are just off the top of my head), `void` functions do > still have an implicit result. > > All of these languages would have had the choice to do what you're > suggesting and use `null`, or its equivalent (`undefined` for > TypeScript and ActionScript). They didn't. Why? If I had to guess, > there's at least three reasons. For one, void is the word languages > usually use for this. For another, `void` and `null` they mean > different things. `void` signifies a function isn't returning > anything. `null` signifies a function that *returns null*, regardless > of where that null came from. `function foo(): null { return > some_probably_null_returning_function(); }` should surely be legal > with a `null` type hint, yet it's nonsensical code. Finally, making a > function truly "return nothing", i.e. disallowing its use as an > expression/rvalue, breaks some use cases, like passing along the > result of a callback. > > PHP would neither be the first nor the last to be using `void` in this > way. > >> If the union types RFC[2] passes it >> makes sense to allow `Foo | null` which allows something of type `Foo` >> or `null`. To me it makes sense that if you then remove `Foo` you are >> left with `null`, not `void`. My personal recommendation because of >> this would be to use `null` for the return type and instead of `void`. > > `null` would be a weird type, because it doesn't make sense as a > parameter type, and as a return type, you don't really want to enforce > returning null, you want to enforce not returning at all (see the > example above). It feels like a poor man's substitute to me. > > Thanks. The tricky part here is that saying a function does not return is not something PHP currently does: https://3v4l.org/HtAuC No return implicitly returns NULL, which you can assign to a variable if, for some strange reason, you were so inclined. So this would be more than "just" a syntactic documentation feature. Which I believe gives the following options: 1) Change the language behavior such that function foo() : void { ...} $a = foo(); Is a syntax error (because there really was nothing returned to assign), rather than resulting in $a having a value of NULL. 2) Use null as a "type" (which I agree feels weird just saying it), such that: function foo() : null { ...} $a = foo(); and function foo() { ...} $a = foo(); are identical. The former would impact the contents of the function (eg, a non-empty return would be a parse error), but the external result is the same ($a == NULL). 3) Use the "void" keyword, but give it the same effect as option 2. The RFC currently seems to propose option 3 (based on the "Use of void functions in expressions" section). I don't have a strong feeling at this point about which option I'd prefer. --Larry Garfield