Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112671 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78677 invoked from network); 30 Dec 2020 18:40:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 18:40:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 257071804D1 for ; Wed, 30 Dec 2020 10:15:38 -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-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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:15:37 -0800 (PST) Received: by mail-wm1-f43.google.com with SMTP id 3so5408720wmg.4 for ; Wed, 30 Dec 2020 10:15:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=xMlSp6juRlDNJU10ISwxpQxht4lFDutCFePyxo3PLxw=; b=Iw08ZLnEg4hosIo8/SlEWm9ADZlcJwJEGxgT6vnF+nHaey52SEi15ICszpTMRyyc1r I/iKuWycNYUa9fe3DMOY1EPpNhQdNxY0TbsoncqGwA5Ng0nBLIMSw8g8iBnmoB+qVznf WyJlDe89hBqQdo49IfxmxtDgt1VJCcf+BFH/j7RPTiPoz3k8w9//XIL4qLMhpojcqBt2 Jq22eRJqU/x8JUbQfxMiae84xLZOZ8RaqqLerdd7zzZXEn4qwlXVfJ1/kz1GhOQ5nY2n tHoDhA11zoraDWlN+kUB4ynXR3TOGUlFAUAzldF5jpP7YIjBXEp1/LLI+z0+xO9eTMNs FuXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=xMlSp6juRlDNJU10ISwxpQxht4lFDutCFePyxo3PLxw=; b=aNxVzFBlBAlVdRrn37B9VO9y86Kl2mby88wWpHxkurlR2oUH6qOPqB6lOZqHIB4mYX x8ki03dvnw4laTcDOf5c7bu+LcZ9eTdy9D6ega0/6Fulx9BqzaWlEWP90g7oJw9jRsNA n3qpTtcPPot8USYBwXij1f/sQO5U555EpJ8lHALNZISXc/izvuJWBAMYrG05/Osg/GZg I2ijU+ezbFwHmYEKVPXd5gv58EsVX5cxGVJzl3LIX1CQpssIF14cUb20WYy2KZuGR4Kx Gmdv8/OObpCEH+MEqEncGPOHiJRkNpWU4YgNuE3VuOvQSEYh41U1YzDdok7DmM5ToBoJ BcQw== X-Gm-Message-State: AOAM532MFbY1MhJ1DpL0AYPInejl9x4pQAfud7uoI1GKTlmv4hwP2Yev jotjCOUHMrACdKDOdY6GXLNGa8af7ltjvg== X-Google-Smtp-Source: ABdhPJzaa8gQ+YlPW/7KM9TDuY67rQyDfzDuJPjSPzjlkBRpIeRGGqahJ0QBBLIA80rB5TGLXdmV1w== X-Received: by 2002:a1c:5459:: with SMTP id p25mr8299110wmi.19.1609352134436; Wed, 30 Dec 2020 10:15:34 -0800 (PST) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id o83sm8150095wme.21.2020.12.30.10.15.33 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 30 Dec 2020 10:15:33 -0800 (PST) To: internals@lists.php.net References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> <459d7ff7-e553-dce9-7d43-c3b1e772e572@gmail.com> Message-ID: <7f4fe9ca-1c20-6f69-cef0-a9718af742a3@gmail.com> Date: Wed, 30 Dec 2020 18:15:31 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: rowan.collins@gmail.com (Rowan Tommins) On 30/12/2020 13:49, Olle Härstedt 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 = new Builder(); > $b = $b->withFoo()->withBar()->withBaz(); > myfun($b); // $b is immutable, so $b cannot be modified by myfun() > return $b; > ``` > > ``` > // Uniqueness > $b = 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 = 1; $end = $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 = 1; $end = clone $start; $end += 5; // where += 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 = MyDate::today(); $end = $start->withAddedDays(5); vs $start = MyDate::today(); $end = 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 = (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. 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 = clone $foo with { x: 42 } to $foo->x = 42, even if the clone is actually in a "withX" method. Regards, -- Rowan Tommins [IMSoP]