Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79457 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29214 invoked from network); 6 Dec 2014 14:26:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Dec 2014 14:26:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.172 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.172 mail-wi0-f172.google.com Received: from [209.85.212.172] ([209.85.212.172:48262] helo=mail-wi0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 40/A1-14211-10213845 for ; Sat, 06 Dec 2014 09:26:10 -0500 Received: by mail-wi0-f172.google.com with SMTP id n3so1149005wiv.11 for ; Sat, 06 Dec 2014 06:26:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=kNMMLgpM/l6sgwLYNfzWDoLY+wnh/X33kNXY3oJSrVM=; b=hnZNBTbYgeF5PVFDRwooHYJBlj/yLtnLm+7kdb6ZThWF1qeRHyOas3DQtc1HuBVGWs MnDBnzdv8zYMe0BQBkkKZ/dQrgbUWy70pV6SnslScGeicctz3qwuzHq7WNCM2xBAWA33 Hql2NEsNwEqR57+JM0ADQWLjfe23uNhr6j0eknt6ZY+jOsMuJCtHQefDVGLmy9osFONh Rj8/49FlG6ECKl1WpZWrRXibuE4vlnHe/3ZN611u9TnvviQVw4qz01FmTHlrIe6bTYT+ 38lS+/NxVC1IVtJhIIlYhe4qMRspvxjbkMgiH4rLq14dl8VPSEBTlVbCTQUaO2S/1XiF jbLg== X-Received: by 10.194.6.227 with SMTP id e3mr32377078wja.107.1417875966616; Sat, 06 Dec 2014 06:26:06 -0800 (PST) Received: from [192.168.0.2] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id h14sm2063163wic.8.2014.12.06.06.26.05 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Dec 2014 06:26:05 -0800 (PST) Message-ID: <548311E6.9010406@gmail.com> Date: Sat, 06 Dec 2014 14:25:42 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: "internals@lists.php.net" References: <2D560BB1-318F-461F-96CD-BE25C346E14F@gmail.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: Only variables can be passed by reference From: rowan.collins@gmail.com (Rowan Collins) On 06/12/2014 03:03, Yasuo Ohgaki wrote: > Hi Rowan, > > On Fri, Dec 5, 2014 at 6:12 PM, Rowan Collins > wrote: > > The author of function f1() presumably designed it to apply some > change to $v, rather than executing only for its side effects and > return value. If the caller wants to ignore this design, they can, > but they are not using the function as it was designed. If they > pass a function return straight into it, they have no way of > capturing the change to $v which the author intended them to see. > > > The value supplied to f1() is return value. > In languages, there are literal, constant and variable. Return value > is variable. No, a return value is a value - it is the result of some computation which has not yet been assigned to a variable. This is perhaps more obvious with an operator: "1 + 2" is not a variable, it is the value 3; internally, a temporary variable is probably used to hold that value, but that temporary variable is never accessible to the programmer. If you have function add($a, $b) { return $a + $b; }, then add(1,2) is still not a variable in any meaningful sense. > It's better to keep basic rule. IMHO. > > $top = array_pop(f2()); > > is better than > > $v = f2(); > $top = array_pop($v); > > Is there anyone who likes latter? array_pop() is a stack-manipulation function, so the latter is the correct use, with $v going on to be used as the rest of the stack. If you're using it for something other than stack manipulation, neither is correct, because it's the wrong choice of function. > Are there any other languages behave like PHP? In some languages - e.g. Pascal, Transact-SQL - the most common form of sub-routine is a "procedure", which doesn't return a value in the way that a function does, but instead has "input" and "output" parameters. Supplying something other than a variable to an "output" parameter would clearly be just as wrong as putting it on the left-hand side of an assignment (e.g. f2() = f1()). Even in languages with functions or methods rather than procedures as their main building block, it's common to have output-only parameters to capture multiple return values without the need for a complex return type. For instance, it might be legal to pass the return value of malloc() in C straight to something requiring a pointer to some memory, but it's almost certainly a mistake to do so, because you would have no way of accessing the buffer afterwards. -- Rowan Collins [IMSoP]