Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112696 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79564 invoked from network); 31 Dec 2020 13:02:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Dec 2020 13:02:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 498181804D1 for ; Thu, 31 Dec 2020 04:37:32 -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=-0.2 required=5.0 tests=BAYES_20,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-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.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 ; Thu, 31 Dec 2020 04:37:31 -0800 (PST) Received: by mail-wr1-f52.google.com with SMTP id c5so20008765wrp.6 for ; Thu, 31 Dec 2020 04:37:31 -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=WgjgBJJjIZViLCqU1j88Im+vjwmnJ5KktALC2QLvO+g=; b=L8ODe39k5+5doJ87DEA8WvZ2l9KpNwVFMe6/r5C9ol17imfLTCjAua6ReHVfaFrfk9 EmQVnEnUcQ8Xz60QkaB/rAlXZc530Obdpxcn0EhqkVtQYSfCCekzOAJQJGqeOl6VsfEd KcAECilJQVXTRXu1XXR5cnTf6O/L0w0EyacUiLcvKhllrpJEufdJgJRwvLj0CHqrQ7Xq kFO3jCQzNBnmtki72YgfH9lyPEG//JLvD/v0qTahn4VAqrSyUQY4kvB6hQ7oC6xVan4d qEe8x2CX2AOiNWN4YE0LKrUw2ld+lMUDzEO3BBRmOavPlehxazoxQTa8Il9XfG8zX8L0 q4GA== 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=WgjgBJJjIZViLCqU1j88Im+vjwmnJ5KktALC2QLvO+g=; b=TgDKelNvSM7nAajCEJgsXV74+ZPhj1aC9fMozDCCygz2HIQqBWnkzPLEo739cl7qCS 3SN48CBSiQfv+9FN67XJsdNaduPYUtx+DOmCKuyUjtLYK21rXYFiDSfRfXV8s87Ybn3F S45NaIo8Cyas1K9GNgaFT5nPyhIWTe1KVk6SpTOABwrZCMgGvd6/tLw59c/5Nr7f+xrU umpuH700zf5Hkxkb33AjiA9ILmdlWpb55zKD7Znml7i+AxuV9yEjw18pJ4JoaQkMU3Ah VbXnD0TmRsgK4GIo9araYTcNJQJ+cEzjJklmGBDU4VO/XhW0z8c+PJ6DAs4EKWJ9U4w1 DNIg== X-Gm-Message-State: AOAM532ZirWlym/jodFE+Fq7unfzlz23YRG3kFd8zSjlm0FMjqkMdoJT ALjZfWXecyMijOzmyG+PA+TP/avY2OjCpA== X-Google-Smtp-Source: ABdhPJzDfV+NyEL/aSoZAwV+lSvBxFMs8/tZvg4zIH6I80v11k13IcfLLMj5pscWugLIMTfl1Ltw6g== X-Received: by 2002:adf:94c7:: with SMTP id 65mr62892292wrr.423.1609418250410; Thu, 31 Dec 2020 04:37:30 -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 v11sm69296007wrt.25.2020.12.31.04.37.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 31 Dec 2020 04:37:29 -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> <7f4fe9ca-1c20-6f69-cef0-a9718af742a3@gmail.com> Message-ID: <30906866-1971-8395-05a0-fd78d054bb89@gmail.com> Date: Thu, 31 Dec 2020 12:37:29 +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) Hi Mike and Olle, On 31/12/2020 00:24, Mike Schinkel wrote: > 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. I wonder if that's just about the choice of names, rather than the mutability/immutability itself? >> $start = MyDate::today(); >> $end = $start->withAddedDays(5); >> >> vs >> >> $start = MyDate::today(); >> $end = clone $start; >> $end->addDays(5); > Ignoring that you are comparing apples and oranges (scalars to objects,) the latter is easier to reason about IMO. Ignoring the distinction between "scalar" and "object" was kind of the point: they are both "values", and are more naturally treated the same as differently. To take a different example, consider writing a new number type (for arbitrary precision, or complex numbers, or whatever), with an "add" method. The mutable version looks something like this: public function add($other) {     $this->value = $this->value + $other; } and has to be used like this: $start = new MyNumber(1); $end = clone $start; $end->add(5); The immutable version might look more like this: public function add($other) {     return clone $this with { value: $this->value + $other }; } and is used like this: $start = new MyNumber(1); $end = $start->add(5); That's much closer to the "$end = $start + 5;" we're used to. On 30/12/2020 18:42, Olle Härstedt wrote: >> 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); > Using a variable on right-hand side does not automatically create an > alias, so in the above case you don't have to use clone. Whether or not the type system forced you to, you'd have to use clone if the values were implemented as mutable. Switching to methods again may make that clearer: $c = new MyNumber(299_792_458); $m = new MyNumber(10); $e = $m->multiply( $c->square() ); If multiply() and square() are mutating state, rather than returning new instances, $c is now 89875517873681764, which is going to totally mess up the universe... Regards, -- Rowan Tommins [IMSoP]