Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112676 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87631 invoked from network); 30 Dec 2020 19:10:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 19:10:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BC0211804D1 for ; Wed, 30 Dec 2020 10:45:07 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (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 ; Wed, 30 Dec 2020 10:45:07 -0800 (PST) Received: by mail-lf1-f51.google.com with SMTP id a12so39560782lfl.6 for ; Wed, 30 Dec 2020 10:45:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Wtv7aW80Yv1OwblLD1CvkKjB7iGcXUoj/4I8MRnt41s=; b=Gb1ed/vKznGFQ5+qScnxoBqryteemV40fA/RTBWQCwMRBfLWhw2toYSwL2RCsW8z1D LtJKuj9KNu3OvxAKf8bKUMRZQE/rmIkTiBFBKbfXSFNqJTIODwVSIRmpu3Yxd0roHP7T suIVuJb+K3M7MIE56C+CW8eE+6DQ3v8ddDbomBiF8yPjdkGS14XlruEo+IYecQ/vNMBA 8X5WWS2x/2ziCxfE6eLLmUKyt5nHrrYxJ2/AmlDOBY2hMhluyMsC7AEGCQQpgxXFdQS3 2/vHgZ7YFNNsJR12WZEP7C3FFP+wAHfK4LRVBh37/LYdKURHW+Hp/1U9NZOoypOVbNOA ClRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Wtv7aW80Yv1OwblLD1CvkKjB7iGcXUoj/4I8MRnt41s=; b=VLx7iouU6eh7LEkI8I35zr19gLtXtaQsynbFM92MCiD75pF7YoJhxXJsNn9uXW02Ld 8KOFU4AmItbSSij4v6YqzhmKQfQE7YtYzGw1wXP85KFf3PilSBp4c7xX91sWh2ZjnDuc ov+M74ibDV3ZzgNn+cq6ZArHiNL+QPngEXjhf/Gx+wrWMkiyU8drliz/foEXf15gvpU6 vCxp18uAM+2rJfQ5fvAGZx0GPz80NpfGxBqutrHgi8JnlAZs32Ng5dYgATwLs95I4t9I m2zi9qW96BiOEX0tvXQsOac/0yb6blJ3Wo4fZBrZi+gQ2Z111/6KynYQ39u81E3Yi0Ul 9mcA== X-Gm-Message-State: AOAM532WDKC1OEWkRiSsIlJIolGQcYzuYzIsCtDnCbQzxkUD8eg6gilL cnEq1f67sDbsPFgwDOqhSyITfs8OHFnppJ1lx8R9xSDYvDw= X-Google-Smtp-Source: ABdhPJzh/1KPAb8ThGVbfZoF5iu5EdOhaoDahXNFtbj9plrVFioddR8ZG9JQSKrlnFrsBUdf6RX40SnzZwk8D/U/nJc= X-Received: by 2002:a05:6512:34d3:: with SMTP id w19mr21375537lfr.418.1609353905706; Wed, 30 Dec 2020 10:45:05 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Wed, 30 Dec 2020 10:45:05 -0800 (PST) In-Reply-To: <3e683903-66cb-4a1a-9ff8-22887dbb8dce@www.fastmail.com> References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> <459d7ff7-e553-dce9-7d43-c3b1e772e572@gmail.com> <7f4fe9ca-1c20-6f69-cef0-a9718af742a3@gmail.com> <3e683903-66cb-4a1a-9ff8-22887dbb8dce@www.fastmail.com> Date: Wed, 30 Dec 2020 18:45:05 +0000 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-12-30 18:31 GMT, Larry Garfield : > On Wed, Dec 30, 2020, at 12:15 PM, Rowan Tommins wrote: >> On 30/12/2020 13:49, Olle H=C3=A4rstedt wrote: >> > Uniqueness is when you only allow _one_ reference to an object (or >> > bucket of memory). >> > [...] >> > >> > You can compare a builder pattern with immutability vs non-aliasing >> > (uniqueness): >> > >> > ``` >> > // Immutable >> > $b =3D new Builder(); >> > $b =3D $b->withFoo()->withBar()->withBaz(); >> > myfun($b); // $b is immutable, so $b cannot be modified by myfun() >> > return $b; >> > ``` >> > >> > ``` >> > // Uniqueness >> > $b =3D new Builder(); // class Builder is annotated as >> > non-aliasing/unique >> > $b->addFoo(); >> > $b->addBar(); >> > $b->addBaz(); >> > myfun(clone $b); // HAVE TO CLONE TO NOT THROW EXCEPTION. >> > return $b; >> > ``` >> >> >> Thanks, I can see how that solves a lot of the same problems, in a very >> robustly analysable way. >> >> However, from a high-level user-friendliness point of view, I think >> "withX" methods are actually more natural than explicitly cloning >> mutable objects. >> >> Consider the case of defining a range: firstly, with plain integers and >> familiar operators: >> >> $start =3D 1; >> $end =3D $start + 5; >> >> This models integers as immutable values, and + as an operator which >> returns a new instance. If integers were mutable but not aliasable, we >> would instead write something like this: >> >> $start =3D 1; >> $end =3D clone $start; >> $end +=3D 5; // where +=3D would be an in-place modification, not a >> short-hand for assignment >> >> I think the first more naturally expresses the desired algorithm. It's >> therefore natural to want the same for a range of dates: >> >> $start =3D MyDate::today(); >> $end =3D $start->withAddedDays(5); >> >> vs >> >> $start =3D MyDate::today(); >> $end =3D clone $start; >> $end->addDays(5); >> >> >> To put it a different way, value types naturally form *expressions*, >> which mutable objects model clumsily. It would be very tedious if we had >> to avoid accidentally mutating the speed of light: >> >> $e =3D (clone $m) * ((clone $c) ** 2); >> >> >> > The guarantee in both above snippets is that myfun() DOES NOT modify >> > $b before returning it. BUT with immutability, you have to copy $b >> > three times, with uniqueness only one. > > That's a good summary of why immutability and with-er methods (or some > equivalent) are more ergonomic. > > Another point to remember: Because of PHP's copy-on-write behavior, full = on > immutability doesn't actually waste that much memory. It does use up som= e, > but far less than you think. (Again, based on the tests MWOP ran for PSR= -7 > a ways back.) I thought copy-on-write was only for arrays, not objects? Olle