Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108711 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59597 invoked from network); 21 Feb 2020 10:21:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Feb 2020 10:21:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ABD1C1801FD for ; Fri, 21 Feb 2020 00:37:20 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 21 Feb 2020 00:37:20 -0800 (PST) Received: by mail-lf1-f53.google.com with SMTP id f24so883127lfh.3 for ; Fri, 21 Feb 2020 00:37:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oK3SQUJIaF1Q3UsxUjOUpZup7aqgE0BjA7SgVTBswLE=; b=oXA9KIvujuIglj9ksxTY6BqfQd33aL2MNn/532/gsR/vD6V2lRl1XDlpk5VTR+/H+Z EbGWdVoUMdkSMQa80ks3e0gDkwMc/0meg4rayl4D+Jxopcg3S5V9ejc0p6ZEG41sWtiy 98cG+zHCEdp7YTV5nHcIyg3JqIIISuKPVTriuBhtf5X6LpFzY9ecFFXFD5XCaUkLAndw x+hYpZ7uYdH26sKzwZ8+nRfKnM0xUw+q1Y/OX56vpD5uAa1GO7y/dER/EzIzV72DofHj NVovvT+Fr+PY9ul5AQZ00f4FpUtuKHYLtjCvpPxTI+v/Hoz+buQYWBhrQGjjiyUGmtQ0 9KPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=oK3SQUJIaF1Q3UsxUjOUpZup7aqgE0BjA7SgVTBswLE=; b=fvTjsFgiQMFzjj2ZaFgOHLF7IrcUvhy/RtpNDZx+Qi0emaPZX57G9fT3PZpxrFMrqj NUE5LMsMozpDBGFhj3xDYl0HLfB3bDjrSvM4HdCgB2idAo7ndiTW2y+z4ySQiHypW8LF buVWQHeyYufJw0d4LdtWy9cRUwUYLosUPgf2JaUV9R1qcZbsP7wRscq1WL1J0utFKZ2K TRy8JcRFToV5lp2bEnok6gOryLcZprJqlfNvS2MJkt3wXYZCGCeFpeoGv/+mGQy9S8IQ id/X7Z1WYG8rZvuTaaKcPjXC5cukUIwqD2UydeFX9utJzZvSabXH3KsGSpj6hSzJH5Tp IwDA== X-Gm-Message-State: APjAAAUm9iF3v8hEdlLbgm/ZZxjAiKru9pYjxAp+1nWqN5mgEK62VC5v /K90U+fhA3sgbTGvKZ72V6lfOAskl5kdQBb9BXek+IdOOdg= X-Google-Smtp-Source: APXvYqymRQ0IF+zl4dtnzVrDC3IrefGmWeTIJCRxKFdfIoouO+TrLCPC/o9wrZWvw+bu4roLb8CQqHV+m+3eJ1aRyb8= X-Received: by 2002:a05:6512:3136:: with SMTP id p22mr7718587lfd.120.1582274237609; Fri, 21 Feb 2020 00:37:17 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 21 Feb 2020 09:37:01 +0100 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000debc41059f11ed72" Subject: Re: [PHP-DEV] [RFC] Explicit call-site pass-by-reference (again) From: nikita.ppv@gmail.com (Nikita Popov) --000000000000debc41059f11ed72 Content-Type: text/plain; charset="UTF-8" On Fri, Feb 21, 2020 at 12:05 AM Larry Garfield wrote: > On Thu, Feb 20, 2020, at 8:47 AM, Levi Morrison via internals wrote: > > Just chiming in to voice strong support for this RFC. This is a key > > piece toward making PHP code statically analyzable. If it becomes > > required at the call site, such as in an edition of the language, it > > will significantly enhance the ability to reason about code and > > probably make it more correct as well. As a small example, consider > > this method on an Optional type class: > > > > function map(callable $f): Optional { > > if ($this->enabled) { > > return new Optional($f($this->data)); > > } else { > > return $this; > > } > > } > > > > The intent is to return a new optional or an empty one, but if you > > pass a closure that accepts something by reference you can change the > > original, which is not intended at all. For people who defend against > > it, it requires saving `$this->data` to a local variable, then passing > > in the local. Then if the user does a call-by-reference it will affect > > the local, not the object's data. > > > If $this->data is itself an object, then you have a concern for data > manipulation (spooky action at a distance) even if it's passed by value. > Given how much data these days is objects, and thus the problem exists > regardless of whether it's by value or by reference passing, adding steps > to make pass-by-reference harder doesn't seem to help much. > If you will allow me some exaggeration, what you're basically saying here is that all the const / readonly / immutability features in (nearly) all programming languages are useless, because they (nearly) always allow for interior mutability in one way or another. "const" in JavaScript doesn't allow you to rebind the object, but you can still modify the object. Same with "final" in Java. Similar things hold in C/C++/Rust when it comes to const pointers/references to structs that contain non-const pointers/references. And of course, the "readonly" RFC for PHP that is currently under discussion has the same characteristics. What I'm trying to say here: All of these features do not guarantee recursive immutability, but that doesn't render them useless in the least. In fact, the outer-most layer is where immutability is the most important, because there's a lot of difference between $i = 0; var_dump($i); // int(0) foo($i); var_dump($i); // array(7) { ... } // WTF just happened??? and $o = new Foo(); var_dump($o); // object(Foo) #42 { xxx } foo($o); var_dump($o); // object(Foo) #42 { yyy } // Did something change in there? Doesn't really matter for this code! One of the big differences is that by-reference passing can change the *type* of the variable, while by-object passing cannot. It cannot even change object identity. On a closing note: I don't think this RFC makes passing by reference "harder" in any meaningful sense. Yes, you do need to write one extra character. In exchange, every time you read code you will immediately see that by-reference passing is used, here be dragons. Regards, Nikita --000000000000debc41059f11ed72--