Re: [RFC] Explicit call-site send-by-ref syntax

From: Date: Sun, 24 Dec 2017 16:45:26 +0000
Subject: Re: [RFC] Explicit call-site send-by-ref syntax
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-101414@lists.php.net to get a copy of this message
Hi Levi, Levi Morrison wrote:
On Wed, Dec 6, 2017 at 12:49 PM, Nikita Popov <nikita.ppv@gmail.com> wrote:
Hi internals, I'd like propose optional support for explicitly marking by-reference argument passing at the call-site, in addition to the declaration-site:
    https://wiki.php.net/rfc/explicit_send_by_ref
In short, while currently we have
    function byRef(&$ref) {...}
    byRef($var);
this proposal would also allow
    function byRef(&$ref) {...}
    byRef(&$var);
so that the use of by-reference passing is obvious without having to consult the function declaration.
I think we ought to commit to requiring the ampersand at the call site some point in the future. As others have noted it provides little benefit at the call site if it is not required. However, there is an area where this does provide value that others have not yet mentioned or thought of: callable parameters.
    public function apply(callable $f) {
        return $f(&$this->data);
    }
This requires the callable to accept the argument by reference, something we cannot currently require. Of course this is rarely needed; I am merely pointing out this feature is more than a syntactic hint to humans. Based on the current discussion I would vote yes on this RFC, despite the concerns raised by others.
Perhaps more useful is the inverse: in future, we could require that a callable *not* take a value by-reference unless the caller asks for it. That would prevent potential monkeying with the scope of the caller by the callee — if right now you do $f($this->data), you might forget that $f could take that parameter by reference and gain the ability to modify that variable indefinitely… Thanks -- Andrea Faulds https://ajf.me/

« previous php.internals (#101414) next »