Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79463 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80071 invoked from network); 7 Dec 2014 09:33:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Dec 2014 09:33:35 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; 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:56603] helo=mail-wi0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EF/10-12155-DEE14845 for ; Sun, 07 Dec 2014 04:33:34 -0500 Received: by mail-wi0-f172.google.com with SMTP id n3so2243202wiv.17 for ; Sun, 07 Dec 2014 01:33:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=user-agent:in-reply-to:references:mime-version :content-transfer-encoding:content-type:subject:from:date:to :message-id; bh=rznOOxK25AqZbeQbkA1IeFO2eFyIfUbw2n9CJBxxBzQ=; b=uAZnCzjQSy2iLq69mP6g3yCUeZOqnL5vnrp60afxz1J8XA8IMJDEOaht/HFEkL48s+ TWSTep6/TG/q8JoXGoVsc6gQkmDRtlXAxMqscv/VybVPsxQuNC0pwez+r/M5G9DtOTkj a1UVlQCvjEXqWb2Z4S0D+hV4VEoI1K/UJaW+km2J1tzNCgfxmBuTStbf0+dlCbyIaXpe 7J4K5Bj1y8c/N4mbkhkt9+q/5tEc4phyJ9gcNipBuM8jKIUqrxdWovUD3NwUgQs4tP2O WUhnwZ2Z9TxnM71Ywb8TGi9SSoSVHfzRdATmgBKSjgYN9/t4NFg+N0tHeMS/7KgOQGXd 1f+w== X-Received: by 10.180.93.37 with SMTP id cr5mr16578433wib.76.1417944809104; Sun, 07 Dec 2014 01:33:29 -0800 (PST) Received: from android-1c899c96c2ec6bf.config (5751a74b.skybroadband.com. [87.81.167.75]) by mx.google.com with ESMTPSA id w10sm51866367wje.10.2014.12.07.01.33.27 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 07 Dec 2014 01:33:28 -0800 (PST) User-Agent: K-9 Mail for Android In-Reply-To: References: <2D560BB1-318F-461F-96CD-BE25C346E14F@gmail.com> <548311E6.9010406@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Date: Sun, 07 Dec 2014 09:33:24 +0000 To: "internals@lists.php.net" Message-ID: <35C29452-6E4B-455F-9084-ECA7180EB234@gmail.com> Subject: Re: [PHP-DEV] Re: Only variables can be passed by reference From: rowan.collins@gmail.com (Rowan Collins) On 6 December 2014 22:25:50 GMT, Yasuo Ohgaki wrote: >Hi Rowan, > >On Sat, Dec 6, 2014 at 11:25 PM, Rowan Collins > >wrote: > >> 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. > > >Value is vague term. I would like to use "expression". Expression is >anything that has value. >Variables/constants are values that have actual storage. >Literals are values that may not have actual storage. > >Joe pointed out PHP treats temporary variable differently. I suppose >temporary variable is created >for values not used as usual variable. e.g. Function return value. > >It may be the thing we need to reconsider. >Temporary variable is an variable after all. It may be used in context >that >is changed then discarded. > >Even if we raise errors for temporary variables, it's better to raise >_the_same_error_ where is it allowed. > >define('MYERROR', -1); >function &f1() {return -1;} // E_NOTICE rather than E_STRICT >function &f2() {return MYERROR;} // E_NOTICE rather than E_STRICT > >$v = f1(); >$v = 2; >var_dump($v); > >$v = f2(); >$v = 2; >var_dump($v); >?> >http://3v4l.org/9LdkK#v551 > >I understand that return by ref and returning literal/constant is >contradictional, yet >it seems too much care for developers. Developer may simply return >value >that >indicates some error in return by ref function should be allowed w/o >error. >IMHO. > > > >> 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. > > >User may use array_slice() for it, but array_pop() is intuitive what it >does and works >perfectly except unneeded error. Therefore, user surprises what is the >"Only variable >references should be ..." means. > >Anyway, I wrote a blog about this spec many years ago and the entry is >accessed >constantly even today. I suppose PHP7 will have more >intuitive/flexible >parsing for >complex expressions(e.g. f()[2]->method(), etc, isn't it?), then this >spec >may confuse >more users. > >We don't need unwanted side effects, therefore this change should be >done >carefully. >I wouldn't argue with this. If removing "Only variable references >should be >..." error is >feasible, I would like to propose this change. You keep talking about these "errors" being a burden on the programmer, but accepting that in principle it would be better to avoid such situations, so I want to reiterate: these are Strict Standards hints - they tell the programmer "we're not going to stop you doing this, but you might want to find a better way to write it". This seems like a perfect balance to me.