Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112691 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31565 invoked from network); 31 Dec 2020 00:50:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Dec 2020 00:50:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CD89E1804CF for ; Wed, 30 Dec 2020 16:24:57 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (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 16:24:57 -0800 (PST) Received: by mail-qv1-f52.google.com with SMTP id l7so8448894qvt.4 for ; Wed, 30 Dec 2020 16:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=mppH2lFSnIYhwnDFIjSTcGRRcouL4wRMCs6lIQSO9xI=; b=kyqZREmdl10MFlBmTPuQw6FDGgEL54KbBrvCPBNDeMea2UDaJ3RiJwGc9x0saLj4Dp C0L4BL8GLJfGzBzgtTRa1NJmCKnxep6GJhhMVTD2M9rZxwawH1BHu8UtScCm6O+3rQTp 4Wia1wA7DX+wA9DJGAhb4NrC+CL75OxNRrNV2wHlCo625NLLErkDW6lSgqO3KAtw9iHc PjuECY9DhPjW5qDWeV7FoGY3BOH2KEX3xRrB5oPWIftZHyBZqrJdiNPwmbPPSSXj54hz 4jGMF1Gl5HYQlZnfbTUPgZX7pxxkWOf+veyUMs4yeOhavEI2bSIMsPyngWFnMKnGWJQM KmEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=mppH2lFSnIYhwnDFIjSTcGRRcouL4wRMCs6lIQSO9xI=; b=MyYua+lkdhgGs1hqTPc2lHnfWPCHFAdjK3C3ujq6j7xW6m49jG/M4v1fYwRjR8fa47 vjxu86LinjalTrqxE/ittxi4996k7BXHbk+sfGhK4Yme69Kznq2uc3tiKBp9lvc8pKhK bRYtnFzUcerdEZfkQZtIdyMSlgvQOM966BU+YVg6k+CzFeWgAF0oE7iKUKuCAbDpVuwk A10XmKriioNA/H8d3mPo/xGucOAig3UIkv5eThqSPHsR05lcTHWRYnMScWdnpPO3QMrq ccKcdhEg4G2RpUKzH35WtEvJUAOre9DDp4zQQG8MqwoQ6NfWVJAUWhvC8hqwu2h4wr64 icpA== X-Gm-Message-State: AOAM530AsnVnR60X7/9oedXvy24McpPxnP996rIoRlYdzzWY3EsA+I1U nyqelVEfBX2capiopIux/5qf/Q== X-Google-Smtp-Source: ABdhPJxoddetKnuTy/SCAbW0oh0e2bmzuwzoSjMLIFufBPUXrncOGMOL8pKKrPzh5A0Cg+1feYi1hw== X-Received: by 2002:a0c:99c8:: with SMTP id y8mr57932122qve.35.1609374295786; Wed, 30 Dec 2020 16:24:55 -0800 (PST) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id i17sm29814359qtg.77.2020.12.30.16.24.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Dec 2020 16:24:54 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: <7f4fe9ca-1c20-6f69-cef0-a9718af742a3@gmail.com> Date: Wed, 30 Dec 2020 19:24:53 -0500 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: 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> To: Rowan Tommins X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: mike@newclarity.net (Mike Schinkel) > On Dec 30, 2020, at 1:15 PM, Rowan Tommins = wrote: >=20 > 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). >> [...] >>=20 >> You can compare a builder pattern with immutability vs non-aliasing >> (uniqueness): >>=20 >> ``` >> // 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; >> ``` >>=20 >> ``` >> // 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; >> ``` >=20 >=20 > Thanks, I can see how that solves a lot of the same problems, in a = very robustly analysable way. >=20 > However, from a high-level user-friendliness point of view, I think = "withX" methods are actually more natural than explicitly cloning = mutable objects. "User-friendliness" of this nature is in the eye of the beholder.=20 A different perspective is that "withX" methods require a mental = translation where "addX" methods do not, much like how a person whose = native language is English will find it a challenge to (or cannot) = "think" in French. > Consider the case of defining a range: firstly, with plain integers = and familiar operators: >=20 > $start =3D 1; > $end =3D $start + 5; >=20 > 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: >=20 > $start =3D 1; > $end =3D clone $start; > $end +=3D 5; // where +=3D would be an in-place modification, not a = short-hand for assignment >=20 > I think the first more naturally expresses the desired algorithm. It's = therefore natural to want the same for a range of dates: >=20 > $start =3D MyDate::today(); > $end =3D $start->withAddedDays(5); >=20 > vs >=20 > $start =3D MyDate::today(); > $end =3D clone $start; > $end->addDays(5); Ignoring that you are comparing apples and oranges (scalars to objects,) = the latter is easier to reason about IMO. > 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: >=20 > $e =3D (clone $m) * ((clone $c) ** 2); >=20 >=20 >> 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. >=20 >=20 > I wonder if that difference can be optimised out by the = compiler/OpCache: detect clones that immediately replace their original, = and optimise it to an in-place modification. In other words, compile = $foo =3D clone $foo with { x: 42 } to $foo->x =3D 42, even if the clone = is actually in a "withX" method. -Mike=